[Tutor] Newbie text file processing question
stuart_clemons@us.ibm.com
stuart_clemons@us.ibm.com
Tue, 7 May 2002 12:35:36 -0400
--0__=0ABBE121DFC6B6308f9e8a93df938690918c0ABBE121DFC6B630
Content-type: text/plain; charset=US-ASCII
Hi all:
I have a question about conditional branching based on the contents of a
text file. I'll try my best to make this understandable !
My file is: foo.txt
The contents of foo.txt are:
(1)lineA
(2) lineA1stuff=eggs
(3) lineABstuff=spam
(4)lineB
(5) lineB1stuff=cars
(6) lineABstuff=trucks
(7) lineB2stuff=books
(8)lineA
(9) lineA1stuff=eggs
(10 lineA2stuff=eggs
I want to do conditional stuff based around if the line is lineA or lineB.
For example if a line in foo.txt equals "lineA", then check that
lineA1stuff equals eggs and lineABstuff equals spam. If a line equals
"lineB", then check that lineB1stuff equals cars and lineABstuff equals
trucks, etc. NOTICE that the contents of line, "lineABstuff", should be
different if it follows line A or line B. (I hope this isn't too confusing
!)
I can more-or-less do what I want using line by line processing and if
statements, but the lines, "lineA" and "lineB" aren't being used for
conditional checking and actions (only the "*stuff" lines are). Is there a
better way to do this ?
Here's an example of the code construct I was thinking of using.
infile = open('f:\\Python22\\foo.txt', 'r')
for line in infile.xreadlines():
if line[3:8] == "lineA":
print "Yes, this is line A", line
if line[7:18] == "lineA1stuff":
if line[19:23]== "eggs":
print "this is correct"
else:
print "this is incorrect"
if line[3:8] == "lineB": , etc, etc, etc
Sorry if this is hopelessly incoherent.
- Stuart
--0__=0ABBE121DFC6B6308f9e8a93df938690918c0ABBE121DFC6B630
Content-type: text/html; charset=US-ASCII
Content-Disposition: inline
<html><body>
<p>Hi all:<br>
<br>
I have a question about conditional branching based on the contents of a text file. I'll try my best to make this understandable !<br>
<br>
My file is: foo.txt<br>
<br>
The contents of foo.txt are:<br>
<br>
(1)lineA<br>
(2) lineA1stuff=eggs<br>
(3) lineABstuff=spam<br>
(4)lineB<br>
(5) lineB1stuff=cars<br>
(6) lineABstuff=trucks<br>
(7) lineB2stuff=books<br>
(8)lineA<br>
(9) lineA1stuff=eggs<br>
(10 lineA2stuff=eggs<br>
<br>
<br>
I want to do conditional stuff based around if the line is lineA or lineB. <br>
<br>
For example if a line in foo.txt equals "lineA", then check that lineA1stuff equals eggs and lineABstuff equals spam. If a line equals "lineB", then check that lineB1stuff equals cars and lineABstuff equals trucks, etc. NOTICE that the contents of line, "lineABstuff", should be different if it follows line A or line B. (I hope this isn't too confusing !)<br>
<br>
I can more-or-less do what I want using line by line processing and if statements, but the lines, "lineA" and "lineB" aren't being used for conditional checking and actions (only the "*stuff" lines are). Is there a better way to do this ? <br>
<br>
Here's an example of the code construct I was thinking of using. <br>
<br>
infile = open('f:\\Python22\\foo.txt', 'r')<br>
for line in infile.xreadlines():<br>
if line[3:8] == "lineA":<br>
print "Yes, this is line A", line<br>
if line[7:18] == "lineA1stuff":<br>
if line[19:23]== "eggs":<br>
print "this is correct"<br>
else: <br>
print "this is incorrect"<br>
if line[3:8] == "lineB": , etc, etc, etc<br>
<br>
Sorry if this is hopelessly incoherent.<br>
<br>
- Stuart<br>
<br>
<br>
</body></html>
--0__=0ABBE121DFC6B6308f9e8a93df938690918c0ABBE121DFC6B630--