python csv阅读器忽略空行

时间:2015-07-20 04:25:19

标签: python csv python-3.x blank-line

我正在使用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)

2 个答案:

答案 0 :(得分:1)

如果您的csv文件以空行开头,我认为在创建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)

答案 1 :(得分:1)

如果你的空白总是在第一行,那么Marius'答案最简单。如果您在开头有n个空白,或者您只想跳过一些行,则可以使用itertools.islice()

先跳过N行

假设您要跳过第一行({1}}行(空行或不行):

4

整个空行

如果您的文件中散布了空行,则可以使用itertools.filterfalse过滤掉它们。

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:
        ...

其中csv1.csv是:

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)

和csv2.csv(注意:此处未显示,但csv2.csv在文件顶部有4个空行):

time,name,location
12345,Jean,Montreal

12346,Peter,Chicago

1234589,Doug,Boston

输出out.csv在整个过程中没有空行:

123457,Scott,San Diego

123458,Jen,Miami

123459,Robert,Sacramento