有谁能够使用 python 的 xlwt 写出 UTF-8 字符吗?

发布于 2024-12-01 07:38:20 字数 488 浏览 1 评论 0原文

我正在尝试将数据写入包含日语字符的 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 技术交流群。

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

发布评论

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

评论(2

无可置疑 2024-12-08 07:38:20

在 Excel 97-2003 XLS 文件中,如果可能的话,每段文本都以 latin1 编码,否则以 UTF-16LE 编码,并用一个标志来显示。为此,xlwt 需要一个 unicode 对象。如果调用者提供 str 对象,xlwt 将尝试使用 Workbook() 调用中指定的编码对其进行解码(默认为 ascii)。

这有效;尝试运行以下简短脚本并使用 Excel 打开生成的文件。

import xlwt
wb = xlwt.Workbook(encoding="UTF-8")
uc = u"".join(unichr(0x0410 + i) for i in xrange(32)) # some Cyrillic characters
u8 = uc.encode("UTF-8")
ws = wb.add_sheet("demo")
ws.write(0, 0, uc)
ws.write(1, 0, u8)
ws.write(2, 0, xlwt.Formula("A1=A2"))
ws.write(3, 0, "ASCII is a subset of UTF-8")
wb.save("xlwt_write_utf8.xls")

事实上,您收到编码错误,而不是解码错误,表明脚本的文件输入部分可能存在问题。请提供导致您收到错误的最短脚本。该脚本应该在失败语句之前包含类似 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, otherwise UTF-16LE, with a flag to show which. To do that, xlwt nees a unicode object. If the caller supplies a str object, xlwt will attempt to decode it using the encoding specified in the Workbook() call (default is ascii).

This works; try running the following short script and open the resultant file with Excel.

import xlwt
wb = xlwt.Workbook(encoding="UTF-8")
uc = u"".join(unichr(0x0410 + i) for i in xrange(32)) # some Cyrillic characters
u8 = uc.encode("UTF-8")
ws = wb.add_sheet("demo")
ws.write(0, 0, uc)
ws.write(1, 0, u8)
ws.write(2, 0, xlwt.Formula("A1=A2"))
ws.write(3, 0, "ASCII is a subset of UTF-8")
wb.save("xlwt_write_utf8.xls")

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.

披肩女神 2024-12-08 07:38:20

正如 this 问题所建议的,将编码设置为工作簿

wb = xlwt.Workbook(encoding='latin-1') 

也应该解决这个问题(它对我有用)。

As suggested by this question, setting the encoding on the WorkBook

wb = xlwt.Workbook(encoding='latin-1') 

should also resolve the issue (it worked for me).

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