在python 2或python 3中写入csv文件的可移植方式

在python 2或python 3中写入csv文件的可移植方式

问题描述:

在我的Windows机器上,我通常是在python 2中执行此操作以编写一个csv文件:

On my Windows box, I usually did this in python 2 to write a csv file:

import csv
f = open("out.csv","wb")
cr = csv.writer(f,delimiter=';')
cr.writerow(["a","b","c"])
f.close()

现在python 3禁止以二进制形式编写文本文件,该代码段不再起作用.可行:

Now that python 3 forbids writing text files as binary, that piece of code does not work anymore. That works:

import csv
f = open("out.csv","w",newline='')
cr = csv.writer(f,delimiter=';')
cr.writerow(["a","b","c"])
f.close()

问题是:Python 2未知newline参数.

Problem is: newline parameter is unknown to Python 2.

当然,省略换行符会导致csv文件中的\r个字符过多,因此不可接受.

Of course, omitting the newline results in a csv file with too many \r chars, so not acceptable.

我目前正在执行向后兼容的过程,以逐步从python 2迁移到python 3.5 我所有的模块中都有很多这样的语句.

I'm currently performing a backwards compatible process to progressively migrate from python 2 to python 3.5 There are a lot of those statements in all my modules.

我的解决方案是将代码嵌入到自定义模块中,然后自定义模块返回文件处理程序+ writer对象.在模块内部进行了python版本检查,这使得使用我的模块的任何模块都可以在任何python版本上正常工作,而不会受到过多的黑客攻击.

My solution was embedding the code in a custom module, and the custom module returns file handler + writer object. A python version check is done inside the module, which allows any module using my module to work whatever python version without too much hacking.

有更好的方法吗?

在Windows上,我发现了python 2&更改csv lineterminator选项(在Windows中以文本模式打开文件时,默认为"\r\n"会使一个\r过多)的3种兼容方式.

On Windows, I found a python 2 & 3 compliant way of doing it changing csv lineterminator option (which defaults to "\r\n" which makes one \r too many when file is open in text mode in Windows)

import csv

with open("out.csv","w") as f:
    cr = csv.writer(f,delimiter=";",lineterminator="\n")
    cr.writerow(["a","b","c"])
    cr.writerow(["d","e","f"])
    cr.writerow(["a","b","c"])
    cr.writerow(["d","e","f"])

无论使用什么python版本,都将创建一个没有臭名昭著的空白行"的csv文件.

Whatever the python version, that will create a csv file without the infamous "blank lines".

唯一的缺点是,在Linux上,此方法将生成不包含\r的文件,这可能不是标准的文件(尽管文件仍然可以在excel中正确打开,没有空行,但仍然有几行:))

The only drawback is that on Linux, this method would produce \r-free files, which is maybe not the standard (although files still opens properly in excel, no blank lines and still several lines :))

问题在3.6.2上仍然存在(就像我应该有一段时间一样检查一下自己)

the problem persists on 3.6.2 (Just checked myself like I should have some time ago)