M
Matt Waite
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. And I'm not sure how to write
the results of this back to the file.
Any help anyone can offer is greatly appreciated. I'm trying hard to
learn, but have been frustrated by this problem.
Matt
Matthew Waite
www.mattwaite.com
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. And I'm not sure how to write
the results of this back to the file.
Any help anyone can offer is greatly appreciated. I'm trying hard to
learn, but have been frustrated by this problem.
Matt
Matthew Waite
www.mattwaite.com