【发布时间】:2011-03-21 21:05:54
【问题描述】:
import csv
with open('thefile.csv', 'rb') as f:
data = list(csv.reader(f))
import collections
counter = collections.defaultdict(int)
for row in data:
counter[row[10]] += 1
with open('/pythonwork/thefile_subset11.csv', 'w') as outfile:
writer = csv.writer(outfile)
for row in data:
if counter[row[10]] >= 504:
writer.writerow(row)
此代码读取thefile.csv,进行更改并将结果写入thefile_subset1。
但是,当我在 Microsoft Excel 中打开生成的 csv 时,每条记录后都会有一个额外的空白行!
有没有办法让它不放额外的空行?
【问题讨论】:
-
请确认当您在 Windows 上运行该代码时会发生这种情况
-
在此线程上查看答案:stackoverflow.com/questions/3348460/…
-
不会将
lineterminator='\n'设置为csv.writer初始化的默认参数解决问题吗?想要有人为此做 Python 3.10 PR 吗? -
顺便说一句,这里是官方的例子:docs.python.org/3/library/csv.html?highlight=csv#examples