python中的默認(rèn)編碼使用
python默認(rèn)編碼
python2中,默認(rèn)使用的是ASCII編碼。
這個(gè)編碼和開(kāi)頭的encoding不同之處在于,開(kāi)頭的encoding是對(duì)于文件內(nèi)容的編碼,默認(rèn)編碼是一些python方法中默認(rèn)使用的編碼。
比如對(duì)str進(jìn)行encode的時(shí)候默認(rèn)先decode的編碼,比如文件寫操作write的encode的編碼等。
python3中默認(rèn)使用的是UTF-8編碼
sys.getdefaultencoding() 獲取默認(rèn)編碼
import sys print(sys.getdefaultencoding())
- python2
D:\SoftInstall\Python\Python38\python3.exe E:/PycharmProjects/displayPY3/1.py
asciiProcess finished with exit code 0
- python3
D:\SoftInstall\Python\Python38\python3.exe E:/PycharmProjects/displayPY3/1.py
utf-8Process finished with exit code 0
sys.setdefaultencoding(編碼格式) 修改默認(rèn)編碼
下面這個(gè)例子,用python2環(huán)境
# coding=utf-8
import sys
print(sys.getdefaultencoding())
reload(sys)
sys.setdefaultencoding('utf-8')
print(sys.getdefaultencoding())
s = '中文'
s.encode('utf-8')
print(s)
#等價(jià)于s.decode(“utf-8”).encode('utf8')E:\PycharmProjects\LEDdisplay2\venv\Scripts\python.exe E:/PycharmProjects/LEDdisplay2/2.py
ascii
utf-8
中文
如果上述代碼沒(méi)有修改默認(rèn)編碼,就會(huì)使用默認(rèn)編碼ASCII來(lái)decode變量s,就會(huì)報(bào)錯(cuò)
# coding=utf-8
import sys
print(sys.getdefaultencoding())
s = '中文'
s.encode('utf-8')
print(s)E:\PycharmProjects\LEDdisplay2\venv\Scripts\python.exe E:/PycharmProjects/LEDdisplay2/2.py
ascii
Traceback (most recent call last):
File "E:/PycharmProjects/LEDdisplay2/2.py", line 5, in <module>
s.encode('utf-8')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)Process finished with exit code 1
上述代碼為什么需要reload(sys)?
請(qǐng)看下面的代碼:
# coding=utf-8
import sys
print(sys.getdefaultencoding())
# reload(sys)
sys.setdefaultencoding('utf-8')
print(sys.getdefaultencoding())
s = '中文'
s.encode('utf-8')
print(s)E:\PycharmProjects\LEDdisplay2\venv\Scripts\python.exe E:/PycharmProjects/LEDdisplay2/2.py
Traceback (most recent call last):
File "E:/PycharmProjects/LEDdisplay2/2.py", line 5, in <module>
sys.setdefaultencoding('utf-8')
AttributeError: 'module' object has no attribute 'setdefaultencoding'
asciiProcess finished with exit code 1
reload是用于重新加載之前import的模塊。
這里需要重新加載sys的原因是:
python在加載模塊時(shí)候刪除了sys中的setdefaultencoding方法(可能是出于安全起見(jiàn)),所以需要reload這個(gè)sys模塊。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python中使用__new__實(shí)現(xiàn)單例模式并解析
單例模式是一個(gè)經(jīng)典設(shè)計(jì)模式,簡(jiǎn)要的說(shuō),一個(gè)類的單例模式就是它只能被實(shí)例化一次,實(shí)例變量在第一次實(shí)例化時(shí)就已經(jīng)固定。 這篇文章主要介紹了Python中使用__new__實(shí)現(xiàn)單例模式并解析 ,需要的朋友可以參考下2019-06-06
Python使用Python-docx庫(kù)實(shí)現(xiàn)Word文檔自動(dòng)化
在日常辦公中,Word文檔處理是高頻需求,無(wú)論是生成報(bào)告、合同,還是批量修改文檔內(nèi)容,手動(dòng)操作效率低下且易出錯(cuò),Python-docx作為Python生態(tài)中處理.docx文件的王牌庫(kù),本文將帶您全面掌握該庫(kù)的核心功能,并附實(shí)戰(zhàn)代碼示例,需要的朋友可以參考下2025-12-12
Python文件監(jiān)聽(tīng)工具pyinotify與watchdog實(shí)例
今天小編就為大家分享一篇關(guān)于Python文件監(jiān)聽(tīng)工具pyinotify與watchdog實(shí)例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10
Python分析彩票記錄并預(yù)測(cè)中獎(jiǎng)號(hào)碼過(guò)程詳解
這篇文章主要介紹了Python分析彩票記錄并預(yù)測(cè)中獎(jiǎng)號(hào)碼過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
Python實(shí)現(xiàn)GIF動(dòng)圖以及視頻卡通化詳解
本文主要介紹了如何使用Python中的animegan2-pytorch實(shí)現(xiàn)動(dòng)圖以及視頻的卡通化效果,文中的代碼具有一定的學(xué)習(xí)價(jià)值,需要的朋友可以參考一下2021-12-12
Python實(shí)現(xiàn)的單向循環(huán)鏈表功能示例
這篇文章主要介紹了Python實(shí)現(xiàn)的單向循環(huán)鏈表功能,簡(jiǎn)單描述了單向循環(huán)鏈表的概念、原理并結(jié)合實(shí)例形式分析了Python定義與使用單向循環(huán)鏈表的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11

