从CSV中删除空白行?

deb*_*ged 17 python csv delete-row

我有一个大的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()
Run Code Online (Sandbox Code Playgroud)

这样可以,还是有更好的方法吗?

Lau*_*ves 22

使用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)
Run Code Online (Sandbox Code Playgroud)

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

if any(row):
Run Code Online (Sandbox Code Playgroud)

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

if any(field.strip() for field in row):
Run Code Online (Sandbox Code Playgroud)

  • @noskio @Paulo:csv文件中的空行可能是空行.例如:''foo,"bar \n \nbaz",quux'`有一个空行,但是是一个单独的csv行. (4认同)
  • @Laurence Gonsalves:**这个答案有一个主要问题:文件应该在BINARY MODE(Python 2.X)中打开,否则在Windows上CR LF处理会混淆结果** (4认同)
  • 呵呵,如果使用`if row.strip()`,相同的代码在没有csv模块的情况下工作 (2认同)
  • +1:CSV非常适合这种情况.这绝不是矫枉过正的. (2认同)

Sag*_*tha 8

很惊讶这里没有人提到pandas。这是一个可能的解决方案。

import pandas as pd
df = pd.read_csv('input.csv')
df.to_csv('output.csv', index=False)
Run Code Online (Sandbox Code Playgroud)


小智 5

用 pandas 来做这件事非常简单。使用 pandas 打开 csv 文件:

import pandas as pd
df = pd.read_csv("example.csv")
#checking the number of empty rows in th csv file
print (df.isnull().sum())
#Droping the empty rows
modifiedDF = df.dropna()
#Saving it to the csv file 
modifiedDF.to_csv('modifiedExample.csv',index=False)
Run Code Online (Sandbox Code Playgroud)


小智 5

使用python从.csv文件中删除空行

    import csv
  ...


 with open('demo004.csv') as input, open('demo005.csv', 'w', newline='') as output:
     writer = csv.writer(output)
     for row in csv.reader(input):
         if any(field.strip() for field in row):
             writer.writerow(row)
Run Code Online (Sandbox Code Playgroud)

谢谢