UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 0: ordinal not in range(128)錯誤解
阿新 • • 發佈:2018-12-18
1. UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 0: ordinal not in range(128)錯誤解決
1.1. 例項:下面程式碼執行出現標題錯誤
#-*- encoding: UTF-8 -*-
name = "梅"
print (u"姓名:%s"%name)
1.2. 解決
① 方式一:新增如下程式碼
#-*- encoding: UTF-8 -*- #匯入import模組 import sys #重新載入sys模組,為什麼必須要載入詳情請看我的第一篇python文章 https://blog.csdn.net/yetugeng/article/details/84836722 reload(sys) #重新設定字符集(此時不會出現提示,別懷疑自己敲錯了) sys.setdefaultencoding("utf-8") name = "梅" print (u"姓名:%s"%name)
② 方式二:可以在Python安裝目錄下的Lib/site-packages目錄中,新建一個sitecustomize.py檔案(建在這裡,每次啟動Python的時候設定將自動生效),內容如下:
import sys
sys.setdefaultencoding('utf-8') #set default encoding to utf-8
兩種方式都可以,推薦第二種,不用每次都設定
---------------------
作者:May_mm
來源:CSDN
原文:https://blog.csdn.net/May_mm/article/details/78278430
版權宣告:本文為博主原創文章,轉載請附上博文連結!