小编典典

要从CSV删除空白行吗?

python

我有一个很大的csv文件,其中某些行完全空白。如何使用Python从csv中删除所有空白行?

经过您的所有建议,这就是我到目前为止的内容

import csv

# open input csv for reading
inputCSV = open(r'C:\input.csv', 'rb')

# create output csv for writing
outputCSV = open(r'C:\OUTPUT.csv', 'wb')

# prepare output csv for appending
appendCSV = open(r'C:\OUTPUT.csv', 'ab')

# create reader object
cr = csv.reader(inputCSV, dialect = 'excel')

# create writer object
cw = csv.writer(outputCSV, dialect = 'excel')

# create writer object for append
ca = csv.writer(appendCSV, dialect = 'excel')

# add pre-defined fields
cw.writerow(['FIELD1_','FIELD2_','FIELD3_','FIELD4_'])

# delete existing field names in input CSV
# ???????????????????????????

# loop through input csv, check for blanks, and write all changes to append csv
for row in cr:
    if row or any(row) or any(field.strip() for field in row):
        ca.writerow(row)

# close files
inputCSV.close()
outputCSV.close()
appendCSV.close()

可以吗?或者有更好的方法吗?


阅读 220

收藏
2020-12-20

共1个答案

小编典典

使用csv模块:

import csv
...

with open(in_fnam) as in_file:
    with open(out_fnam, 'w') as out_file:
        writer = csv.writer(out_file)
        for row in csv.reader(in_file):
            if row:
                writer.writerow(row)

如果还需要删除所有字段为空的行,请将行更改if row:为:

if any(row):

而且,如果您还想将仅包含空格的字段视为空白,则可以将其替换为:

if any(field.strip() for field in row):

请注意,在Python 2.x和更早版本中,该csv模块需要二进制文件,因此您需要使用e'b'标志打开文件。在3.x中,这样做将导致错误。

2020-12-20