On 11/05/2011 03:04 PM, Mayo Adams wrote:
Code to build a list z to contain all the first words in each line of a
text file:

z=[]

f=open('C:/atextfile.txt')

for aLine in f:

str=aLine

a=str.split()

z.append(a)


I would like to work further with the list z once the file has been
stripped, and the list is complete but am so clueless about Python and its
curlybracelessness that I don't know when the for loop terminates.
Obviously I don't want to write to the file after every line is read.


Python's indentation rules are pretty simple. You need to indent all the contents of a for-loop, and the loop is over when you outdent back to line up with the for statement. In your posting, you don't indent at all, so you'll get an error on the line following the for statement.

Now perhaps that's the fault of the way you're posting. Easiest is usually to tell your email program to compose as plain text.

z=[]
f=open('C:/atextfile.txt')
for aLine in f:
    str=aLine
    a=str.split()
    z.append(a)
f.close()
print z


Now you mention writing to a file.  I hope you're not planning to write to the 
same file you're reading from.  Anyway,tell us more about what you want, and 
whether the print of z shows what you're expecting, or want.


--

DaveA

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to