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)

这段代码读取file.csv,进行修改,并将结果写入到file_subset1。

然而,当我在Microsoft Excel中打开结果csv时,每条记录后都有一个额外的空行!

有没有办法让它不放额外的空行?


当前回答

简单的答案是csv文件应该总是以二进制模式打开,无论是输入还是输出,否则在Windows上就会出现行结束的问题。具体来说,在输出时,csv模块将写入\r\n(标准的csv行结束符),然后(在文本模式下)运行时将用\r\n (Windows标准的行结束符)替换\n,给出结果\r\r\n。

摆弄线终结者不是解决方案。

其他回答

在Python 3+中,以二进制模式“wb”打开文件将不起作用。或者说,您必须在写入数据之前将其转换为二进制。那只是个麻烦。

相反,您应该将其保持在文本模式,但将换行符重写为空。像这样:

with open('/pythonwork/thefile_subset11.csv', 'w', newline='') as outfile:
with open(destPath+'\\'+csvXML, 'a+') as csvFile:
    writer = csv.writer(csvFile, delimiter=';', lineterminator='\r')
    writer.writerows(xmlList)

lineterminator='\r'"允许传递到下一行,两行之间没有空行。

注意:这似乎不是首选的解决方案,因为在Windows系统上是如何添加额外的行。如python文档中所述:

如果csvfile是一个文件对象,在不同的平台上,它必须用' b '标志打开。

Windows is one such platform where that makes a difference. While changing the line terminator as I described below may have fixed the problem, the problem could be avoided altogether by opening the file in binary mode. One might say this solution is more "elegent". "Fiddling" with the line terminator would have likely resulted in unportable code between systems in this case, where opening a file in binary mode on a unix system results in no effect. ie. it results in cross system compatible code.

来自Python文档:

On Windows, 'b' appended to the mode opens the file in binary mode, so there are also modes like 'rb', 'wb', and 'r+b'. Python on Windows makes a distinction between text and binary files; the end-of-line characters in text files are automatically altered slightly when data is read or written. This behind-the-scenes modification to file data is fine for ASCII text files, but it’ll corrupt binary data like that in JPEG or EXE files. Be very careful to use binary mode when reading and writing such files. On Unix, it doesn’t hurt to append a 'b' to the mode, so you can use it platform-independently for all binary files.

原:

作为csv可选参数的一部分。如果你得到额外的空行,你可能不得不改变行结束符(信息这里)。下面的例子改编自python页面csv docs。把“\n”改成它应该是什么样子。因为这只是在黑暗中对问题的一种尝试,这可能行得通,也可能行不通,但这是我最好的猜测。

>>> import csv
>>> spamWriter = csv.writer(open('eggs.csv', 'w'), lineterminator='\n')
>>> spamWriter.writerow(['Spam'] * 5 + ['Baked Beans'])
>>> spamWriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])

使用下面定义的方法将数据写入CSV文件。

open('outputFile.csv', 'a',newline='')

只需在open方法中添加一个额外的newline= "参数:

def writePhoneSpecsToCSV():
    rowData=["field1", "field2"]
    with open('outputFile.csv', 'a',newline='') as csv_file:
        writer = csv.writer(csv_file)
        writer.writerow(rowData)

这将写入CSV行,而无需创建额外的行!

简单的答案是csv文件应该总是以二进制模式打开,无论是输入还是输出,否则在Windows上就会出现行结束的问题。具体来说,在输出时,csv模块将写入\r\n(标准的csv行结束符),然后(在文本模式下)运行时将用\r\n (Windows标准的行结束符)替换\n,给出结果\r\r\n。

摆弄线终结者不是解决方案。