Newbie question: replacing nulls in CSV with preceding value
skip at pobox.com
skip at pobox.com
Thu Feb 1 15:37:01 EST 2007
Matt> I have a CSV file, exported from Excel, that has blank records in
Matt> it, and I need to fill them in with the values from the record
Matt> just above it until it hits a non-blank value.
Try something like:
#!/usr/bin/env python
import sys
import csv
last = {}
reader = csv.DictReader(open("test1.csv", "rb"))
writer = csv.DictWriter(open("test2.csv", "wb"),
sys.stdout, fieldnames="Zone City Event".split())
for row in reader:
for key in row:
if not row[key]:
row[key] = last.get(key, "")
writer.writerow(row)
last = row
Skip
More information about the Python-list
mailing list