Newbie question: replacing nulls in CSV with preceding value
Bruno Desthuilliers
bdesth.quelquechose at free.quelquepart.fr
Thu Feb 1 16:46:29 EST 2007
Matt Waite a écrit :
> My first post, my first real python use, please be gentle:
>
> I have a CSV file, exported from Excel, that has blank records in it,
> and I need to fill them in with the values from the record just above
> it until it hits a non-blank value. Here's an example of the data,
> which is in a file called test2.csv:
>
> Zone,City,Event
> 1,Anytown,Event
> ,,Event1
> ,,Event2
> ,,Event44
> 2,Anothertown,Event3
> ,,Event5
> ,,Event7
>
> What I need it to look like is:
>
> Zone,City,Event
> 1,Anytown,Event1
> 1,Anytown,Event2
> 1,Anytown,Event44
> 2,Anothertown,Event3
> 2,Anothertown,Event5
> 2,Anothertown,Event7
>
> Pretty much everything I've tried has failed, and I've been searching
> for hours for something similar and haven't found anything. The best
> I've come up with --which is half baked and I don't even know if it
> works -- is this:
>
> 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.
Did you actually tried it ? And itf yes, do you really think it 'works'?
Ok, the solution is quite easy - assuming the very first data row has no
blank values, and that either both Zone and City are blank or both are
filled:
prev_zone = prev_city = ''
for row in reader:
if row['Zone']:
prev_zone, prev_city = row['Zone'], row['City']
else:
row['Zone'], row['City'] = prev_zone, prev_city
# now do something with row...
> And I'm not sure how to write
> the results of this back to the file.
look for csv.DictWriter.writerow
NB : note that you cannot "write back to the file" - just write to
*another* file (then eventually os.rename(olfile, newfile))
More information about the Python-list
mailing list