Python reading data from input file
<p>I want to read specific data from an input file. How can I read it?</p>

<p>For example my file has data like:</p>

<pre class="lang-none prettyprint-override"><code>this is my first line
this is my second line.
</code></pre>

<p>So I just want to read <code>first</code> from the first line and <code>secon</code> from the second line.</p>

<p>Try the following code for your needs but please read the comments above.</p>

<pre><code># ----------------------------------------
# open text file and write reduced lines
# ----------------------------------------
#this is my first line
#this is my second line.
pathnameIn = "D:/_working"
filenameIn = "foobar.txt"
pathIn = pathnameIn + "/" + filenameIn

pathnameOut = "D:/_working"
filenameOut = "foobar_reduced.txt"
pathOut = pathnameOut + "/" + filenameOut

fileIn = open(pathIn,'r')
fileOut = open(pathOut,'w')

print(fileIn)
print(fileOut)

i = 0

# Save all reduced lines to a file.
for lineIn in fileIn.readlines():
    i += 1 # number of lines read
    lineOut = lineIn[11:16]
    fileOut.writelines(lineOut +"\n") 

print("*********************************")
print("gelesene Zeilen: " + str(i))
print("*********************************")

fileIn.close()
fileOut.close()
</code></pre>

python python-3.x