简体   繁体   English

使用xlsxwriter编写.xlsx文件时出现UnicodeDecodeError错误

[英]UnicodeDecodeError error writing .xlsx file using xlsxwriter

I am trying to write about 1000 rows to a .xlsx file from my python application. 我试图从我的python应用程序写一个.xlsx文件大约1000行。 The data is basically a combination of integers and strings. 数据基本上是整数和字符串的组合。 I am getting intermittent error while running wbook.close() command. 运行wbook.close()命令时出现间歇性错误。 The error is the following: 错误如下:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 15: 
                     ordinal not in range(128)

My data does not have anything in unicode. 我的数据在unicode中没有任何内容。 I am wondering why the decoder is being at all. 我想知道为什么解码器正在存在。 Has anyone noticed this problem? 有人注意到这个问题吗?

0xc3 is "À". 0xc3是“À”。 So what you need to do is change the encoding. 所以你需要做的是改变编码。 Use the decode() method. 使用decode()方法。

string.decode('utf-8')

Also depending on your needs and uses you could add 根据您的需求和用途,您可以添加

# -*- coding: utf-8 -*-

at the beginning of your script, but only if you are sure that the encoding will not interfere and break something else. 在脚本的开头,但前提是您确定编码不会干扰并破坏其他内容。

As Alex Hristov points out you have some non-ascii data in your code that needs to be encoded as UTF-8 for Excel. 正如Alex Hristov所指出的,您的代码中有一些非ascii数据需要编码为Excel的UTF-8。

See the following examples from the docs which each have instructions on handling UTF-8 with XlsxWriter in different scenarios: 请参阅文档中的以下示例,每个示例都有关于在不同场景中使用XlsxWriter处理UTF-8的说明:

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

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