欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python中的默認(rèn)編碼使用

 更新時(shí)間:2024年06月24日 10:18:49   作者:冰美式QAQ  
這篇文章主要介紹了python中的默認(rèn)編碼使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

python默認(rèn)編碼

python2中,默認(rèn)使用的是ASCII編碼。

這個(gè)編碼和開頭的encoding不同之處在于,開頭的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
ascii

Process finished with exit code 0

  • python3

D:\SoftInstall\Python\Python38\python3.exe E:/PycharmProjects/displayPY3/1.py
utf-8

Process 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
中文

如果上述代碼沒有修改默認(rèn)編碼,就會(huì)使用默認(rèn)編碼ASCII來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'
ascii

Process finished with exit code 1

reload是用于重新加載之前import的模塊。

這里需要重新加載sys的原因是:

python在加載模塊時(shí)候刪除了sys中的setdefaultencoding方法(可能是出于安全起見),所以需要reload這個(gè)sys模塊。

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 使用Python實(shí)現(xiàn)圖像標(biāo)記點(diǎn)的坐標(biāo)輸出功能

    使用Python實(shí)現(xiàn)圖像標(biāo)記點(diǎn)的坐標(biāo)輸出功能

    這篇文章主要介紹了使用Python實(shí)現(xiàn)圖像標(biāo)記點(diǎn)的坐標(biāo)輸出功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2019-08-08
  • 淺談keras中l(wèi)oss與val_loss的關(guān)系

    淺談keras中l(wèi)oss與val_loss的關(guān)系

    這篇文章主要介紹了淺談keras中l(wèi)oss與val_loss的關(guān)系,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • Python實(shí)現(xiàn)的一個(gè)簡(jiǎn)單LRU cache

    Python實(shí)現(xiàn)的一個(gè)簡(jiǎn)單LRU cache

    這篇文章主要介紹了Python實(shí)現(xiàn)的一個(gè)簡(jiǎn)單LRU cache,本文根據(jù)實(shí)際需求總結(jié)而來,需要的朋友可以參考下
    2014-09-09
  • Python代碼模擬CPU工作原理

    Python代碼模擬CPU工作原理

    Python代碼來實(shí)現(xiàn)一個(gè)最簡(jiǎn)單的CPU。用代碼模擬大的部件,使大家從原理上理解CPU工作。使它可編程,支持加減法運(yùn)算、讀寫內(nèi)存、無條件跳轉(zhuǎn)、條件跳轉(zhuǎn)的功能。
    2023-01-01
  • pandas中df.groupby()方法深入講解

    pandas中df.groupby()方法深入講解

    在使用pandas進(jìn)行數(shù)據(jù)統(tǒng)計(jì)分析時(shí)遇到了問題,找了很久才找到解決辦法,所以下面這篇文章主要給大家介紹了關(guān)于pandas中df.groupby()方法的相關(guān)資料,需要的朋友可以參考下
    2022-12-12
  • python?subprocess執(zhí)行外部命令常用方法詳細(xì)舉例

    python?subprocess執(zhí)行外部命令常用方法詳細(xì)舉例

    這篇文章主要給大家介紹了關(guān)于python?subprocess執(zhí)行外部命令常用方法的相關(guān)資料,Python的subprocess模塊提供了一種在Python中調(diào)用外部命令的方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-12-12
  • Python2.x與Python3.x的區(qū)別

    Python2.x與Python3.x的區(qū)別

    這篇文章主要介紹了Python2.x與Python3.x的區(qū)別的相關(guān)資料,需要的朋友可以參考下
    2016-01-01
  • Python使用matplotlib顯示圖像實(shí)例

    Python使用matplotlib顯示圖像實(shí)例

    在Python項(xiàng)目中處理圖像數(shù)據(jù)之前,需要確保安裝了matplotlib庫,它是一個(gè)用于繪制圖表和圖像顯示的工具,若尚未安裝,可以使用pip命令進(jìn)行安裝,安裝完成后,可以通過matplotlib的pyplot模塊讀取并顯示MNIST手寫數(shù)據(jù)集中的圖像,若需要顯示灰度圖
    2024-10-10
  • python實(shí)現(xiàn)發(fā)送郵件功能代碼

    python實(shí)現(xiàn)發(fā)送郵件功能代碼

    這篇文章主要介紹了python實(shí)現(xiàn)發(fā)送郵件功能代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • django模型層(model)進(jìn)行建表、查詢與刪除的基礎(chǔ)教程

    django模型層(model)進(jìn)行建表、查詢與刪除的基礎(chǔ)教程

    這篇文章主要給大家介紹了關(guān)于django模型層(model)進(jìn)行建表、查詢與刪除的等基礎(chǔ)操作的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-11-11

最新評(píng)論