pythoncsv.reader,python csv reader忽略空行

Im using the pythons csv reader . How can I use the following code in such a way that it ignores blank lines.

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)

解决方案

If your csv files start with a blank line, I think you should be able to skip that line with readline() before creating the csv reader:

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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值