解决 python用 xlsxwrite 向excel 中写入中文字符串变量 报错

简介:

问题:  UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 7: ordinal not in range(128)


分析:在网上查找,原来python的str默认是ascii编码,和unicode编码冲突,所以无法 中文字符串变量写入excel。


解决:在代码中加入下面几行就可以了。

import sys
reload(sys)
sys.setdefaultencoding('utf8')





      本文转自独弹古调  51CTO博客,原文链接:http://blog.51cto.com/hunkz/1697345,如需转载请自行联系原作者

相关文章
|
18天前
|
Python
1167: 分离字符串(PYTHON)
1167: 分离字符串(PYTHON)
|
23小时前
|
Python
python_读写excel、csv记录
python_读写excel、csv记录
7 0
|
2天前
|
存储 Python
Python变量类型
Python变量类型
8 0
|
2天前
|
Python
Python 字符串格式化指南
本文介绍了Python中的三种字符串格式化方法:1) 使用 `%` 操作符,如 `%s` 和 `%d`;2) `str.format()` 方法,通过 `{}` 占位符插入变量;3) Python 3.6 引入的 f-strings,直接在字符串内嵌入变量。此外,还提到了高级用法,如格式控制(如指定小数位数)。这些方法有助于更有效地处理和格式化字符串输出。
3 0
|
3天前
|
存储 Python
Python的变量与数据类型详解
Python的变量与数据类型详解
11 0
|
7天前
|
数据挖掘 索引 Python
Python 读写 Excel 文件
Python 读写 Excel 文件
12 0
|
8天前
|
索引 Python
Python高维变量选择:SCAD平滑剪切绝对偏差惩罚、Lasso惩罚函数比较
Python高维变量选择:SCAD平滑剪切绝对偏差惩罚、Lasso惩罚函数比较
10 0
|
9天前
|
Python
IDA3.12版本的python,依旧报错IDAPython: error executing init.py.No module named ‘impRefer to the message win
IDA3.12版本的python,依旧报错IDAPython: error executing init.py.No module named ‘impRefer to the message win
|
9天前
|
Python
python学习-函数模块,数据结构,字符串和列表(下)
python学习-函数模块,数据结构,字符串和列表
49 0
|
9天前
05-python之函数-函数的定义/函数的参数/函数返回值/函数说明文档/函数的嵌套使用/函数变量的作用域
05-python之函数-函数的定义/函数的参数/函数返回值/函数说明文档/函数的嵌套使用/函数变量的作用域