有谁能够使用 python 的 xlwt 写出 UTF-8 字符吗?
我正在尝试将数据写入包含日语字符的 Excel 文件。 我正在使用 codec.open() 来获取数据,这似乎工作正常,但是当我尝试写入数据时遇到此错误:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 16-17: ordinal not in range(128)
我不明白为什么程序会坚持在这里使用 ascii 。当我创建一个新的工作簿对象时,我使用了
wb = xlwt.Workbook(encoding='utf-8')
程序文件本身和它正在读取的文件都保存为 UTF-8。
有人有什么想法吗?
编辑:这是 xlwt 包的链接。 http://pypi.python.org/pypi/xlwt
I'm trying to write data to an excel file that includes Japanese characters.
I'm using codec.open() to get the data, and that seems to work fine, but I run into this error when I try to write the data:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 16-17: ordinal not in range(128)
I don't understand why the program would be insisting on using ascii here. When I created a new workbook object, I did so using
wb = xlwt.Workbook(encoding='utf-8')
and both the program file itself and the file it's reading in are saved as UTF-8.
Anybody have any ideas?
EDIT: Here's a link to the xlwt package. http://pypi.python.org/pypi/xlwt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Excel 97-2003 XLS 文件中,如果可能的话,每段文本都以
latin1
编码,否则以UTF-16LE
编码,并用一个标志来显示。为此,xlwt 需要一个unicode
对象。如果调用者提供str
对象,xlwt 将尝试使用 Workbook() 调用中指定的编码对其进行解码(默认为ascii
)。这有效;尝试运行以下简短脚本并使用 Excel 打开生成的文件。
事实上,您收到编码错误,而不是解码错误,表明脚本的文件输入部分可能存在问题。请提供导致您收到错误的最短脚本。该脚本应该在失败语句之前包含类似
print repr(your_utf8_text)
的内容,以便我们可以准确地看到文本数据是什么。请包含完整的错误消息和完整的回溯,以及非常短的输入文件的内容 (print repr(contents)
)。In an Excel 97-2003 XLS file, each piece of text is encoded in
latin1
if that is possible, otherwiseUTF-16LE
, with a flag to show which. To do that, xlwt nees aunicode
object. If the caller supplies astr
object, xlwt will attempt to decode it using the encoding specified in the Workbook() call (default isascii
).This works; try running the following short script and open the resultant file with Excel.
The fact that you are getting an encode error, not a decode error, indicates a possible problem in the file input part of your script. Please supply the shortest possible script that causes the error that you are getting. The script should contain something like
print repr(your_utf8_text)
immediately prior to the failing statement, so that we can see exactly what the text data is. Please include the full error message and the full traceback, and the contents (print repr(contents)
) of your very short input file.正如 this 问题所建议的,将编码设置为工作簿
也应该解决这个问题(它对我有用)。
As suggested by this question, setting the encoding on the WorkBook
should also resolve the issue (it worked for me).