![](/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.