自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Python print編碼轉(zhuǎn)變默認編碼

開發(fā) 后端
Python print編碼如何進行相關(guān)的編碼應(yīng)用。尤其是在默認編碼轉(zhuǎn)換的過程中有不少的問題,下面我們就先詳細的看看相關(guān)問題。

Python print編碼有很多的應(yīng)用范圍,在這里我們先來看看如何進行默認編碼轉(zhuǎn)變成為系統(tǒng)編碼。希望大家有所收獲。什么情況用encode,什么情況又是decode呢,剛開始總是被搞昏。

其實各種本地字符集的英文名是Coded Character Set,要轉(zhuǎn)換為Coded,肯定是要encode了,同樣,從里面解出來也應(yīng)該叫decode……

decode就是把其他編碼轉(zhuǎn)換為unicode,等同于unicode函數(shù);encode就是把unicode編碼的字符串轉(zhuǎn)換為特定編碼。在pyshell里繼續(xù):

a是Str類型的,所以再用encode會報錯。用print輸出時會調(diào)用默認編碼轉(zhuǎn)換為系統(tǒng)編碼?

Python print編碼

  1. >>> a.decode("gb2312")   
  2. u'\u6211'   
  3. >>> print a.decode("gb2312")   
  4. >>> a.encode("gb2312")   
  5. Traceback (most recent call last):   
  6. File "<input>", line 1, in ?   
  7. UnicodeDecodeError: 'ascii' codec can't decode byte 0xce in 
    position 0: ordinal not in range(128)   

 

b是unicode類型,打印時需要先encode(編碼)成系統(tǒng)編碼

Python 代碼

  1. >>> print b.encode("gb2312")   
  2. >>> b.encode("gb2312")   
  3. '\xce\xd2'   
  4. >>> b.decode("gb2312")   
  5. Traceback (most recent call last):   
  6. File "<input>", line 1, in ?   
  7. UnicodeEncodeError: 'ascii' codec can't encode character 
    u'\u6211' in position 0: ordinal not in range(128)  

Python print編碼里默認的encode和decode是strict模式,所以會直接拋出Error,而Java里是默認replace模式,所以在處理servlet時經(jīng)常會看到一串?????在decode時傳入第二個參數(shù)errors為'replace'可以和Java相同,但總是沒成功,還不知道為什么。

【編輯推薦】

  1. Python script代碼在C++中的使用方法
  2. Python next函數(shù)實際操作教程
  3. Python正則表達式的幾種匹配方法
  4. Python圖像處理的好處所在
  5. Python多線程具體運用的方法
責(zé)任編輯:張浩 來源: CSDN
相關(guān)推薦

2010-12-31 13:44:55

python

2015-09-28 14:27:12

硬編默認選擇

2010-03-23 14:34:44

Python vim檢

2017-06-23 15:13:28

EPC編碼對象

2010-02-01 10:21:36

Python編碼轉(zhuǎn)換

2015-07-31 09:27:56

安全編碼代碼審計Python

2022-12-13 07:55:00

Python地理編碼

2016-02-15 14:13:39

Python編碼環(huán)境

2012-01-13 12:21:45

Java

2023-11-09 19:12:43

AIoTel視頻編碼

2013-04-08 10:33:54

編碼編碼規(guī)范

2009-06-08 19:52:47

Eclipse字符編碼

2015-09-29 10:07:58

中文編碼

2024-03-04 07:50:04

Python字符編碼網(wǎng)絡(luò)通信

2017-03-25 21:32:40

Python編碼

2018-01-22 17:02:48

Python字符編碼ASCII

2024-02-28 23:07:42

GolangBase64編碼

2023-04-13 00:24:00

前端編碼JavaScrip

2020-11-05 10:20:54

前端編碼規(guī)范安全漏洞

2015-03-18 09:34:47

程序員編碼規(guī)范
點贊
收藏

51CTO技術(shù)棧公眾號