我正在使用python csv阅读器。我如何以忽略空白行的方式使用以下代码。
import csv f1 = open ("ted.csv") oldFile1 = csv.reader(f1, delimiter=',', quotechar='"') oldList1 = list(oldFile1) f2 = open ("ted2.csv") newFile2 = csv.reader(f2, delimiter=',', quotechar='"') newList2 = list(newFile2) f1.close() f2.close() with open("ted.csv") as f1, open("ted2.csv") as f2, open('foo.csv', 'w') as out: r1, r2 = csv.reader(f1), csv.reader(f2) st = set((row[0], row[3]) for row in r1) wr = csv.writer(out) for row in (row for row in r2 if (row[0],row[3]) not in st): wr.writerow(row)
如果您的csv文件以空行开头,我认为您应该能够readline()在创建csv阅读器之前跳过该行:
readline()
with open("ted.csv") as f1, open("ted2.csv") as f2, open('foo.csv', 'w') as out: f1.readline() f2.readline() r1, r2 = csv.reader(f1), csv.reader(f2)