我在标题之前处理带缓冲行的csv文件,行数有所不同,有些行包含字符串,有些行不包含字符串。唯一一致的是这些缓冲区行在一个或多个单元格中都包含空值,所以我试图跳过任何具有空单元格的行。
我已经获得了以下脚本,但它正在输出一个空白文件。我猜测我没有到达其他地方'但我猜测如果我把它放在一个循环中,我将最终为每一行创建一个文件...
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
答案 0 :(得分:4)
你的代码做了一个的事情;要么它会读取并丢弃(跳过)一个行,然后返回,或它将读取整个文件并复制到新的CSV。它不会同时做到这两点。
如果您不能指望包含标题的第一行,那么请不要依赖于自动加载文件中的DictReader()
字段名。手动查找标题,然后将其传递给DictReader()
构造函数。
首先以常规csv.reader()
打开CSV,找到第一行作为实际标题,然后将该文件重新加载为csv.DictReader()
,该行为字段名显式:
with open(fileName, 'rb') as inf, open("out_"+fileName, 'wb') as outf:
reader = csv.reader(inf)
# find header row
for row in reader:
if '' not in row:
fieldnames = row
break
else:
# oops, *only* rows with empty cells found
raise ValueError('Unable to determine header row')
# rewind, switch to DictReader, skip past header
inf.seek(0)
reader = csv.DictReader(inf, fieldnames)
for row in reader:
if row.keys() == row.values()
break
# copy all rows across with extra two columns
writer = csv.DictWriter(outf, ['url_source','downloaded_at'] + fieldnames)
writer.writeheader()
writer.writerows(dict(r, url_source=csvUrl, downloaded_at=today)
for r in reader)