"groupby" is brilliant!
Alex Martelli
aleax at mac.com
Wed Jun 14 10:59:04 EDT 2006
Frank Millman <frank at chagford.com> wrote:
> 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.
BTW, if and when you do need a list for some other purpose,
rows = list(reader)
may be slightly better than the for/append loop; and if you need a
sorted list, perhaps
rows = sorted(reader)
similarly.
Alex
More information about the Python-list
mailing list