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时,每条记录后都有一个额外的空行!

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


当前回答

借用这个答案,似乎最干净的解决方案是使用io.TextIOWrapper。我为自己解决了这个问题:

from io import TextIOWrapper

...

with open(filename, 'wb') as csvfile, TextIOWrapper(csvfile, encoding='utf-8', newline='') as wrapper:
    csvwriter = csv.writer(wrapper)
    for data_row in data:
        csvwriter.writerow(data_row)

上面的答案与Python 2不兼容。为了具有兼容性,我认为只需要将所有的写入逻辑包装在if块中:

if sys.version_info < (3,):
    # Python 2 way of handling CSVs
else:
    # The above logic

其他回答

注意:这似乎不是首选的解决方案,因为在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'])

借用这个答案,似乎最干净的解决方案是使用io.TextIOWrapper。我为自己解决了这个问题:

from io import TextIOWrapper

...

with open(filename, 'wb') as csvfile, TextIOWrapper(csvfile, encoding='utf-8', newline='') as wrapper:
    csvwriter = csv.writer(wrapper)
    for data_row in data:
        csvwriter.writerow(data_row)

上面的答案与Python 2不兼容。为了具有兼容性,我认为只需要将所有的写入逻辑包装在if块中:

if sys.version_info < (3,):
    # Python 2 way of handling CSVs
else:
    # The above logic

csv。Writer模块直接控制行结束符,并直接将\r\n写入文件。在python3中,该文件必须以非翻译文本模式打开,参数为'w', newline= "(空字符串),否则在Windows上将写入\r\r\n,其中默认文本模式将每个\n转换为\r\n。

#!python3
with open('/pythonwork/thefile_subset11.csv', 'w', newline='') as outfile:
    writer = csv.writer(outfile)

在Python 2中,使用二进制模式以'wb'而不是'w'模式打开outfile,以防止Windows换行符转换。Python 2在Unicode方面也有问题,需要其他变通方法来编写非ascii文本。如果你必须处理在Python 2上将Unicode字符串写入csv,请参阅下面的Python 2链接以及页面末尾的unicoderreader和UnicodeWriter示例,或者查看第三方unicodecsv模块:

#!python2
with open('/pythonwork/thefile_subset11.csv', 'wb') as outfile:
    writer = csv.writer(outfile)

文档链接

https://docs.python.org/3/library/csv.html#csv.writer https://docs.python.org/2/library/csv.html#csv.writer

我使用writerow

def write_csv(writer, var1, var2, var3, var4):
    """
    write four variables into a csv file
    """
    writer.writerow([var1, var2, var3, var4])      
        
numbers=set([1,2,3,4,5,6,7,2,4,6,8,10,12,14,16])
rules = list(permutations(numbers, 4))
#print(rules)
selection=[]
with open("count.csv", 'w',newline='') as csvfile:
    writer = csv.writer(csvfile)

    for rule in rules:
        number1,number2,number3,number4=rule
        if ((number1+number2+number3+number4)%5==0):
            #print(rule)
            selection.append(rule)
            write_csv(writer,number1,number2,number3,number4)

我将这个答案w.r.t.写入python 3,因为我最初遇到了相同的问题。

我应该使用PySerial从arduino获取数据,并将它们写入.csv文件中。在我的例子中,每次读取都以'\r\n'结束,因此换行符总是分隔每行。

在我的例子中,newline= "选项不起作用。因为它显示了一些错误,比如:

with open('op.csv', 'a',newline=' ') as csv_file:

ValueError: illegal newline value: ''

所以他们似乎不接受省略换行符。

只在这里看到一个答案,我提到了writer对象中的行结束符,比如,

Writer = csv。Writer (csv_file, delimiter=' ',lineterminator='\r')

这对我跳过额外的换行很有效。