Python csv writer 分隔符错误?
免责声明:我在欧洲。
根据 此页 Excel 使用分号 ;
作为欧洲的默认分隔符,以“防止与小数逗号发生冲突”。
现在,我有这个 Python 代码:
import csv
data = [["test", "data"], ["foo", "bar"]]
writer = csv.writer(open("data.csv", "wb"), dialect="excel")
writer.writerows(data)
应该生成这个文件:
test;data
foo;bar
但它使用逗号。为什么会发生这种情况? locale.getdefaultlocale()
返回('nl_NL', 'cp1252')
。
Disclaimer: I'm in Europe.
According to this page Excel uses the semicolon ;
as default separator in Europe to "prevent conflicts" with the decimal comma.
Now, I have this Python code:
import csv
data = [["test", "data"], ["foo", "bar"]]
writer = csv.writer(open("data.csv", "wb"), dialect="excel")
writer.writerows(data)
Which should generate this file:
test;data
foo;bar
but instead it uses commas. Why is this happening? locale.getdefaultlocale()
returns ('nl_NL', 'cp1252')
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为 csv.excel 方言不支持区域设置。如果您希望显式使用分号作为分隔符,那么您需要将分隔符显式传递给 csv.open as
或创建一个新的方言并注册它
无论哪种情况,您都应该测试浮点数的写入方式......我怀疑它们不会以您想要的欧洲格式编写(以逗号为基数)
This is because the csv.excel dialect is not locale aware. If you wish to explicitly use semicolons as the delimiter then you need to either explicitly pass the delimiter to csv.open as
or create a new dialect and register it
In either case, you should test how floating point numbers are written ... I suspect they won't be written in the European format you desire (with a comma as the radix)
excel
方言由以下属性指定(在Lib/csv.py
中,第 57 行):我没有看到任何提示,这在某种程度上取决于语言环境 - 因此你总是会得到
,< /code> 使用默认方言。
但这很容易解决,例如
The
excel
dialect is specified by the following attributes (inLib/csv.py
, line 57):I see no hint, that this is somehow locale-dependent - hence you'll always get
,
with the default dialect.But that's easily fixed, e.g.