简体   繁体   English

xlwt模块 - 保存xls unicode错误

[英]xlwt module - saving xls unicode error

I'm trying to save some text with xlwt module, creating new xls document and saving text there.So far it worked great, until I came across unicode text: for example simple string '80°'. 我正在尝试使用xlwt模块保存一些文本,创建新的xls文档并在那里保存文本。到目前为止它工作得很好,直到我遇到unicode文本:例如简单的字符串'80°'。

When I call book.save('simple.xls') I get UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 .Is there any way I can avoid that? 当我调用book.save('simple.xls')我得到UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 。我有什么方法可以避免这种情况吗?

Instead of writing a regular string, write a Unicode string. 编写Unicode字符串而不是编写常规字符串。 For example, instead of 例如,而不是

ws.write(r, c, '80°')

do

ws.write(r, c, '80°'.decode('cp1252'))

(Of course, pick the appropriate encoding for your data.) (当然,为您的数据选择合适的编码。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM