Benji York wrote:
> Frank Millman wrote:
> > reader = csv.reader(open('trans.csv', 'rb'))
> > rows = []
> > for row in reader:
> > rows.append(row)
>
> Why do you create a list of rows instead of just iterating over the
> reader directly?
> --
> Benji York
A - didn't think of it - good idea
B - can't always do it -
B1 - if the file is not sorted, I have to sort the rows first
B2 - if I need to update the file, I can modify the rows in place, and
then call
csv.writer(open('trans.csv','wb')).writerows(rows)
BTW, I know that B2 is simplistic - to be safe I should rename, then
write, then unlink. I will do that for production code.
BTW2, an alternative to B2 is
reader = csv.reader(open('trans.csv', 'rb'))
newtrans = open('newtrans.csv','wb')
writer = csv.writer(newtrans)
for row in reader:
[process and modify row]
writer.writerow(row)
newtrans.close()
[unlink and rename]
Could be useful if the file is large. Food for thought.
Thanks
Frank
--
http://mail.python.org/mailman/listinfo/python-list