使用欧元符号分隔符将 pandas 写入 csv 时出错 [英] Error writing pandas to csv with euro sign delimiter
问题描述
我正在尝试将 Pandas 数据框写入以€"作为分隔符的 csv.
I'm trying to write a Pandas data frame to a csv with '€' as a delimiter.
data.to_csv(file_path, sep = '€')
但是,我收到错误:
TypeError: "delimiter" must be an 1-character string
我该如何克服这个问题?我必须使用欧元符号作为分隔符
How can I overcome this issue? I must use the euro sign as a delimiter
推荐答案
是时候切换到 python 3了.
这是因为在 python 2 中默认字符串是 ascii.ASCII 字符是 1 个字节(因此它们的范围是 0 到 127).这不足以代表所有字符;在这种情况下'€'.表示这需要 3 个字节.
This is because by default string is ascii in python 2. ASCII characters are 1 byte (hence their range is 0 to 127). This is not sufficient to represent all characters; in this case '€'. To represent this 3 bytes is needed.
因此世界切换到 unicode.哪个范围更大.幸运的是,python3 的所有字符串都使用 unicode.
在 python3 data.to_csv(file_path, sep = '€')
将正常工作.
Hence the world switched to unicode. Which has higher range. Luckily python3 uses unicode for all its string.
In python3 data.to_csv(file_path, sep = '€')
will work properly.
这篇关于使用欧元符号分隔符将 pandas 写入 csv 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!