Newbie question: replacing nulls in CSV with preceding value

Laszlo Nagy gandalf at designaproduct.biz
Thu Feb 1 15:43:20 EST 2007


> import csv
> citynew=''
> reader = csv.DictReader(open("/home/mwaite/test/test2.csv", "rb"))
> for row in reader:
>     row['CITY'] == citynew
> else:
>     citynew=row['CITY']
>
> The logic here -- in this case trying to fill in the city information
> -- would seem to work, but I'm not sure. And I'm not sure how to write
> the results of this back to the file.
>   
I recommend that you create a writer and a new file. After you processed 
the input, you can delete the original file and replace it with the new 
one. My experience is that this is highly efficient and works with very 
large files. I would do something like this (not tested)

import csv
import os
import shutil
fin = file("/home/mwaite/test/test2.csv", "rb")
reader = csv.DictReader(fin)
fout = file(fin.name + '.tmp',"wb+")
writer = csv.writer(fout)
prev = {}
columns = ('CITY','ZIP','NAME')
for curr in reader:
   foreach col in columns:
      if (not curr.has_key(col)) or (curr[col] == ''):
	if prev.has_key(col):
	   curr[col] = prev[col]
	else:
	   curr[col] = ''
   row = [curr[col] for col in columns ]
   writer.write(row)
   prev = curr
os.unlink(fin.name)
fout.close()
shutil.rename(fout.name,fin.name)

I really did not test it, but you can get the idea.

Best,

   Laszlo






More information about the Python-list mailing list