从 CSV 中删除空白行?
我有一个很大的 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()
这可以吗?或者有更好的方法吗?
I have a large csv file in which some rows are entirely blank. How do I use Python to delete all blank rows from the csv?
After all your suggestions, this is what I have so far
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()
Is this ok or is there a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
使用
csv
模块:如果您还需要删除所有字段均为空的行,请将
if row:
行更改为:如果您还想处理字段仅由空格组成的空文件,您可以将其替换为:
请注意,在 Python 2.x 及更早版本中,
csv
模块需要二进制文件,因此您需要打开文件带有 e'b'
标志。在 3.x 中,这样做会导致错误。Use the
csv
module:If you also need to remove rows where all of the fields are empty, change the
if row:
line to:And if you also want to treat fields that consist of only whitespace as empty you can replace it with:
Note that in Python 2.x and earlier, the
csv
module expected binary files, and so you'd need to open your files with e'b'
flag. In 3.x, doing this will result in an error.令人惊讶的是这里没有人提到
pandas
。这是一个可能的解决方案。Surprised that nobody here mentioned
pandas
. Here is a possible solution.使用python从.csv文件中删除空行
谢谢
Delete empty row from .csv file using python
Thankyou
用 pandas 来做这件事非常简单。使用 pandas 打开 csv 文件:
Doing it with pandas is very simple. Open your csv file with pandas:
您必须打开第二个文件,向其中写入所有非空行,删除原始文件并将第二个文件重命名为原始名称。
编辑:真正的空白行将类似于“\n”:
包含所有空白字段的行将类似于“,,,,,\n”。如果您认为这是一个空行:
打开、关闭、删除和重命名文件就留给您作为练习。 (提示:导入 os、help(open)、help(os.rename)、help(os.unlink))
EDIT2:Laurence Gonsalves 引起我的注意,有效的 csv 文件可能在引用的 csv 字段中嵌入空行,例如 < code>1, '这\n\很棘手',123.45。在这种情况下,csv 模块将为您处理这个问题。对不起,劳伦斯,你的回答应该被接受。 csv 模块还将解决像
"","",""\n
这样的行的问题。You have to open a second file, write all non blank lines to it, delete the original file and rename the second file to the original name.
EDIT: a real blank line will be like '\n':
a line with all blank fields would look like ',,,,,\n'. If you consider this a blank line:
openning, closing, deleting and renaming the files is left as an exercise for you. (hint: import os, help(open), help(os.rename), help(os.unlink))
EDIT2: Laurence Gonsalves brought to my attention that a valid csv file could have blank lines embedded in quoted csv fields, like
1, 'this\n\nis tricky',123.45
. In this case the csv module will take care of that for you. I'm sorry Laurence, your answer deserved to be accepted. The csv module will also address the concerns about a line like"","",""\n
.用于从 csv 文件中删除空白行而不创建另一个文件的 python 代码。
def ReadWriteconfig_file(文件):
python code for remove blank line from csv file without create another file.
def ReadWriteconfig_file(file):
这是一个使用 pandas 删除空白行的解决方案。
Here is a solution using pandas that removes blank rows.
不幸的是,我需要这样做,但没有像此代码那样在 CSV 文件末尾写入空白行(如果您保存 -> .csv,这也是 Excel 所做的)。我使用 CSV 模块的(更简单的)代码也执行此操作:
M51_csv_proc.csv 正好有 125 行;该程序始终输出 126 行,最后一行为空。
我已经经历过所有这些线程,似乎没有什么可以改变这种行为。
I need to do this but not have a blank row written at the end of the CSV file like this code unfortunately does (which is also what Excel does if you Save-> .csv). My (even simpler) code using the CSV module does this too:
M51_csv_proc.csv has exactly 125 rows; the program always outputs 126 rows, the last one being blank.
I've been through all these threads any nothing seems to change this behaviour.
在此脚本中,所有 CR / CRLF 均从 CSV 文件中删除,然后具有如下行:
执行脚本 https://github.com/eoconsulting/lr2excelcsv/blob/master/lr2excelcsv.py
结果(Excel CSV 格式):
In this script all the CR / CRLF are removed from a CSV file then has lines like this:
Execute the script https://github.com/eoconsulting/lr2excelcsv/blob/master/lr2excelcsv.py
Result (in Excel CSV format):
将 PATH_TO_YOUR_CSV 替换为您的
或 内联:
Replace the PATH_TO_YOUR_CSV with your
or in-line:
我也有同样的问题。
我将 .csv 文件转换为数据帧,然后将数据帧转换回 .csv 文件。
带有空行的初始 .csv 文件是 'csv_file_logger2.csv' 。
所以,我做了以下过程
I had the same, problem.
I converted the .csv file to a dataframe and after that I converted the dataframe back to the .csv file.
The initial .csv file with the blank lines was the 'csv_file_logger2.csv' .
So, i do the following process