"groupby" is brilliant!

Jon Clements joncle at googlemail.com
Tue Jun 13 17:09:58 EDT 2006


Not related to itertools.groupby, but the csv.reader object...

If for some reason you have malformed CSV files, with embedded newlines
or something of that effect, it will raise an exception. To skip those,
you will need a construct of something like this:

raw_csv_in = file('filenamehere.csv')
for raw_line in raw_csv_in:
    try:
        # Do something to rawline here maybe if necessary to "clean it
up"
        row = csv.reader( [raw_line] ).next()
        # Do your stuff here
    except csv.Error:
        pass # or do something more appropriate if the record is
important

May not be applicable in your case, but has stung me a few times...

All the best,

Jon.


Frank Millman wrote:
> Paul McGuire wrote:
> > >
> > > reader = csv.reader(open('trans.csv', 'rb'))
> > > rows = []
> > > for row in reader:
> > >     rows.append(row)
> > >
> >
> > This is untested, but you might think about converting your explicit "for...
> > append" loop into either a list comp,
> >
> >     rows = [row for row in reader]
> >
> > or just a plain list constructor:
> >
> >     rows = list(reader)
> >
> > Neh?
> >
> > -- Paul
> >
>
> Yup, they both work fine.
>
> There may be times when you want to massage the data before appending
> it, in which case you obviously have to do it the long way. Otherwise
> these are definitely neater, the last one especially.
>
> You could even do it as a one-liner -
>     rows = list(csv.reader(open('trans.csv', 'rb')))
> 
> It still looks perfectly readable to me.
> 
> Thanks
> 
> Frank




More information about the Python-list mailing list