[Tutor] Adding to a CSV file?
aeneas24 at priest.com
aeneas24 at priest.com
Mon Aug 30 19:04:48 CEST 2010
I checked out the csv module and got a little further along, but still can't quite figure out how to iterate line by line properly.
# This shows that I'm reading the file in correctly:
input_file=open("test-8-29-10.csv","rb")
for row in input_file:
print row
MyWord,Category,Ct,CatCt
!,A,2932,456454
!,B,2109,64451
a,C,7856,90000
abandoned,A,11,456454
....
# But when I try to add columns, I'm only filling in some static value. So there's something wrong with my looping.
testReader=csv.reader(open('test-8-29-10.csv', 'rb'))
for line in testReader:
for MyWord, Category, Ct, CatCt in testReader:
text=nltk.word_tokenize(MyWord)
word2=wnl.lemmatize(word)
word3=porter.stem(word)
print MyWord+","+Category+","+Ct+","+CatCt+","+word+","+word2+","+word3+"\r\n"
!,A,2932,456454,yrs,yr,yr
!,B,2109,64451,yrs,yr,yr
a,C,7856,90000,yrs,yr,yr
abandoned,A,11,456454,yrs,yr,yr
...
# I tried adding another loop, but it gives me an error.
testReader=csv.reader(open('test-8-29-10.csv', 'rb'))
for line in testReader:
for MyWord, Category, Ct, CatCt in line: # I thought this line inside the other was clever, but, uh, not so much
text=nltk.word_tokenize(MyWord)
word2=wnl.lemmatize(word)
word3=porter.stem(word)
print MyWord+","+Category+","+Ct+","+CatCt+","+word+","+word2+","+word3+"\r\n"
Traceback (most recent call last):
File "<pyshell#256>", line 2, in <module>
for MyWord, Category, Ct, CatCt in line:
ValueError: too many values to unpack
My hope is that once I can figure out this problem, it'll be easy to write the csv file with the csv module. But I'm stumped about the looping.
Thanks for any suggestions,
Tyler
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100830/54c8f94b/attachment.html>
More information about the Tutor
mailing list