有没有人能够使用python的xlwt来写出UTF-8字符? [英] Has anyone been able to write out UTF-8 characters using python's xlwt?

查看:457
本文介绍了有没有人能够使用python的xlwt来写出UTF-8字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数据写入包含日语字符的Excel文件中. 我正在使用codec.open()来获取数据,这似乎可以正常工作,但是在尝试写入数据时遇到了此错误:

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)

我不明白为什么程序会在这里坚持使用ascii.当我创建一个新的工作簿对象时,我使用了

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')

以及程序文件本身和正在读取的文件都保存为UTF-8.

and both the program file itself and the file it's reading in are saved as UTF-8.

有人有什么主意吗?

这是xlwt软件包的链接. http://pypi.python.org/pypi/xlwt

Here's a link to the xlwt package. http://pypi.python.org/pypi/xlwt

推荐答案

在Excel 97-2003 XLS文件中,如果可能的话,每段文本均以latin1编码,否则为UTF-16LE,并带有标记以显示哪个.为此,xlwt需要一个unicode对象.如果调用方提供了str对象,则xlwt将尝试使用Workbook()调用中指定的编码(默认值为ascii)对它进行解码.

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).

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

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")

您得到的是编码错误而不是解码错误,这表明脚本的文件输入部分可能存在问题.请提供最短的脚本,该脚本会导致您遇到错误.该脚本应该在失败的语句之前包含print repr(your_utf8_text)之类的内容,以便我们可以准确地看到文本数据是什么.请包括完整的错误消息和完整的追溯,以及非常简短的输入文件的内容(print repr(contents)).

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.

这篇关于有没有人能够使用python的xlwt来写出UTF-8字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆