Python csv writer 分隔符错误?

发布于 2024-12-04 12:37:39 字数 610 浏览 0 评论 0原文

免责声明:我在欧洲。

根据 此页 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沉溺在你眼里的海 2024-12-11 12:37:39

这是因为 csv.excel 方言不支持区域设置。如果您希望显式使用分号作为分隔符,那么您需要将分隔符显式传递给 csv.open as

writer = csv.writer(open("data.csv", "wb"), delimiter=";")

或创建一个新的方言并注册它

class excel_semicolon(csv.excel):
    delimiter = ';'
register_dialect("excel-semicolon", excel_semicolon)

无论哪种情况,您都应该测试浮点数的写入方式......我怀疑它们不会以您想要的欧洲格式编写(以逗号为基数)

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

writer = csv.writer(open("data.csv", "wb"), delimiter=";")

or create a new dialect and register it

class excel_semicolon(csv.excel):
    delimiter = ';'
register_dialect("excel-semicolon", excel_semicolon)

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)

等风来 2024-12-11 12:37:39

excel 方言由以下属性指定(在 Lib/csv.py 中,第 57 行):

delimiter = ','
quotechar = '"'
doublequote = True
skipinitialspace = False
lineterminator = '\r\n'
quoting = QUOTE_MINIMAL

我没有看到任何提示,这在某种程度上取决于语言环境 - 因此你总是会得到 ,< /code> 使用默认方言。

但这很容易解决,例如

class excel_semicolon(csv.excel):
    delimiter = ';'

writer = csv.writer(open("data.csv", "wb"), dialect=excel_semicolon)

The excel dialect is specified by the following attributes (in Lib/csv.py, line 57):

delimiter = ','
quotechar = '"'
doublequote = True
skipinitialspace = False
lineterminator = '\r\n'
quoting = QUOTE_MINIMAL

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.

class excel_semicolon(csv.excel):
    delimiter = ';'

writer = csv.writer(open("data.csv", "wb"), dialect=excel_semicolon)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文