![](/img/trans.png)
[英]Python - UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 1070: character maps to <undefined>
[英]UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position X: character maps to <undefined>
当我尝试使用 pip 安装StringGenerator<\/a>时,系统提示我以下错误:
C:\Users\Administrator> pip install StringGenerator
Collecting StringGenerator
Using cached StringGenerator-0.3.0.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\ADMINI~1\AppData\Local\Temp\2\pip-build-mdvrj2cf\StringGenerator\setup.py", line 7, in <module>
long_description = file.read()
File "c:\users\administrator\appdata\local\programs\python\python36-32\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 1264: character maps to <undefined>
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in C:\Users\ADMINI~1\AppData\Local\Temp\2\pip-build-mdvrj2cf\StringGenerator\
问题是在设置过程中读取README.txt
。 在 Windows 中,默认编码为 cp1252,但该自述文件很可能以 UTF8 编码。
错误消息告诉您 cp1252 编解码器无法解码字节为 0x9D 的字符。 当我浏览自述文件时,我发现了这个字符: ”
(也称为:“右双引号”),其中包含字节 0xE2 0x80 0x9D ,其中包括有问题的字节。
你可以做的是:
来自:
with open('README.txt') as file:
long_description = file.read()
改成:
with open('README.txt', encoding="utf8") as file:
long_description = file.read()
这将使用正确的编码打开文件。
或者,您可以完全删除这两行并删除long_description=long_description,
位于setup()
内的第 18 行。
python setup.py install
由于 setup.py 脚本中没有实际设置,您可以直接从GitHub克隆源文件夹,该包应该仍然可以正常工作。
只需在“open('path', here)”中添加encoding="utf8"
" 即可。
with open('path to csv file', encoding="utf8") as csv_file:
转到https://pypi.python.org/pypi/StringGenerator/0.3.0并下载最新版本(或本例中的源),解压 .gz 文件,然后解压 .tar 文件。
接下来进入StringGenerator-0.2.0
文件夹并打开终端并运行:
python setup.py build
python setup.py install
或者从 PowerShell 运行:
python ./setup.py build
python ./setup.py install
我在 Windows 版本的 python 上
pip install<\/code>时也遇到了这个问题。
解决方案是设置以下环境变量:
PYTHONUTF8=1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.