csv blank fields

jkv jkv at unixcluster.dk
Sat Jun 27 11:00:46 EDT 2009


Mag Gam wrote:

> > well, I am actually loading the row into a fixed width array
> >
> > reader=csv.reader(fs)
> > for s,row in enumerate(reader):
> >   t=np.array([(row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8],row[9],row[10])],dtype=mtype)
> >   d[s]=t
> >
> >
> > If there is a missing field, I get a problem in one of my rows
> >   
>   

I had a similar problem, my problem was that in my source data (csv
file) sometimes the last cell was left out, so i ended up doing a try to
check if the row existed, if not i appended to the row.

Something like:

try:
    row[11]
except IndexError:
    row.append("")

If you want to insert a value into empty cells you can do something like
this:

if row[10] == ''
    row[10] = 'NULL'



-- 
Regards,
jkv
http://unixcluster.dk/public.key




More information about the Python-list mailing list