I'm handling csv files with buffer rows before the header, the number of rows varies and some of the rows contain strings and some don't. The only thing that is consistent is that these buffer rows all contain a null value in one or more of the cells, so I'm trying to skip any row with a null cell.
I've got the following script but it is outputting a blank file. I'm guessing that I'm not getting to the 'else' but I'm guessing that if I put it in a loop I'll end up creating a file for every row...
with open(fileName, 'rb') as inf, open("out_"+fileName, 'wb') as outf:
csvreader = csv.DictReader(inf)
if '' in csvreader.fieldnames:
next(csvreader)
else:
fieldnames = ['url_source','downloaded_at'] + csvreader.fieldnames # add column names to beginning
csvwriter = csv.DictWriter(outf, fieldnames)
csvwriter.writeheader()
for node, row in enumerate(csvreader, 1):
csvwriter.writerow(dict(row, url_source=csvUrl, downloaded_at=today))
return
fieldnamesfails?