python csv reader 忽略空行
python csv reader ignore blank row
我正在使用 pythons csv reader。我如何以忽略空行的方式使用以下代码。
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 文件以空行开头,我认为您应该能够在创建 csv reader:
之前使用 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)
如果你的空格总是在第一行,那么 Marius 的答案是最简单的。如果您在开头有 n
个空格,或者您只想跳过一些行,您可以使用 itertools.islice()
.
跳过前 N 行
假设您想跳过前 4
行(是否为空行):
from itertools import islice
with open('csv2.csv', 'r') as f1, open('out.csv', 'w') as out:
filt_f1 = islice(f1, 4, None)
r1 = csv.reader(filt_f1)
wr = csv.writer(out)
for line in r1:
...
全空行
如果您的文件中散布着空行,那么您可以使用 itertools.filterfalse
过滤掉它们。
import csv
from itertools import filterfalse
from itertools import chain
with open('csv1.csv', 'r') as f1, open('csv2.csv', 'r') as f2, open('out.csv', 'w') as out:
# create an iterator without lines that start with '\n'
filt_f1 = filterfalse(lambda line: line.startswith('\n'), f1)
filt_f2 = filterfalse(lambda line: line.startswith('\n'), f2)
# csv.reader consumes the filtered iterators
r1, r2 = csv.reader(filt_f1), csv.reader(filt_f2)
wr = csv.writer(out)
# here insert your logic, I just write both to the same file
for line in chain(r1, r2):
wr.writerow(line)
其中 csv1.csv 是:
time,name,location
12345,Jean,Montreal
12346,Peter,Chicago
1234589,Doug,Boston
和csv2.csv(注意:此处未显示,但csv2.csv文件顶部有4个空行:
123457,Scott,San Diego
123458,Jen,Miami
123459,Robert,Sacramento
输出out.csv全程无空行:
time,name,location
12345,Jean,Montreal
12346,Peter,Chicago
1234589,Doug,Boston
123457,Scott,San Diego
123458,Jen,Miami
123459,Robert,Sacramento
我正在使用 pythons csv reader。我如何以忽略空行的方式使用以下代码。
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 文件以空行开头,我认为您应该能够在创建 csv reader:
之前使用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)
如果你的空格总是在第一行,那么 Marius 的答案是最简单的。如果您在开头有 n
个空格,或者您只想跳过一些行,您可以使用 itertools.islice()
.
跳过前 N 行
假设您想跳过前 4
行(是否为空行):
from itertools import islice
with open('csv2.csv', 'r') as f1, open('out.csv', 'w') as out:
filt_f1 = islice(f1, 4, None)
r1 = csv.reader(filt_f1)
wr = csv.writer(out)
for line in r1:
...
全空行
如果您的文件中散布着空行,那么您可以使用 itertools.filterfalse
过滤掉它们。
import csv
from itertools import filterfalse
from itertools import chain
with open('csv1.csv', 'r') as f1, open('csv2.csv', 'r') as f2, open('out.csv', 'w') as out:
# create an iterator without lines that start with '\n'
filt_f1 = filterfalse(lambda line: line.startswith('\n'), f1)
filt_f2 = filterfalse(lambda line: line.startswith('\n'), f2)
# csv.reader consumes the filtered iterators
r1, r2 = csv.reader(filt_f1), csv.reader(filt_f2)
wr = csv.writer(out)
# here insert your logic, I just write both to the same file
for line in chain(r1, r2):
wr.writerow(line)
其中 csv1.csv 是:
time,name,location
12345,Jean,Montreal
12346,Peter,Chicago
1234589,Doug,Boston
和csv2.csv(注意:此处未显示,但csv2.csv文件顶部有4个空行:
123457,Scott,San Diego
123458,Jen,Miami
123459,Robert,Sacramento
输出out.csv全程无空行:
time,name,location
12345,Jean,Montreal
12346,Peter,Chicago
1234589,Doug,Boston
123457,Scott,San Diego
123458,Jen,Miami
123459,Robert,Sacramento