python字符串編碼解碼的使用
python通過(guò)ord©獲取字符c的unicode的編碼值,為整數(shù)。通過(guò)chr(i)獲取i對(duì)應(yīng)的unicode的字符。通過(guò)str.encode()將字符串編碼為原始字節(jié),b.decode()將原始字節(jié)解碼為字符串。
1 字符串基礎(chǔ)知識(shí)
python通過(guò)ord©獲取字符c的unicode的編碼值,為整數(shù)。
通過(guò)chr(i)獲取i對(duì)應(yīng)的unicode的字符。
1.1 字符編碼方法
ASCII碼定義0-127的字符代碼,每個(gè)字符存儲(chǔ)在一個(gè)8位的字節(jié)中。
| # | 內(nèi)置函數(shù) | 描述 |
|---|---|---|
| 1 | ord© | 返回字符c的unicode的編碼值,為整數(shù) |
| 2 | chr(i) | 返回unicode編碼為i的字符 |
| 3 | help(encodings) | 先import encodings,再help,查看python支持的編碼名稱 |
從字符串編碼為原始字節(jié),從原始字節(jié)解碼字符串。
| # | 名稱 | 描述 |
|---|---|---|
| 1 | 編碼 | 根據(jù)編碼名稱,把字符串翻譯為原始字節(jié) |
| 2 | 解碼 | 根據(jù)編碼名稱,把原始字節(jié)翻譯為字符串 |
示例
>>> ord('梯')
26799
>>> chr(26799)
'梯'
1.2 python字符串類型
| # | 版本 | 描述 |
|---|---|---|
| 1 | py2.x | str表示8位文本和二進(jìn)制數(shù)據(jù) |
| 2 | unicode表示寬字符unicode文本 | |
| 3 | py3.x | str表示unicode文本 |
| 4 | bytes表示二進(jìn)制數(shù)據(jù) | |
| 5 | bytearray,可變的bytes類型 |
1.3 文本和二進(jìn)制文件
| # | 模式 | 描述 |
|---|---|---|
| 1 | bytes和二進(jìn)制模式 | 處理圖像文件、解壓的打包數(shù)據(jù)、設(shè)備數(shù)據(jù)流 |
| 2 | str和文本模式 | 用于程序輸出、HTML、CSV、XML文件 |
2 python3.0的字符串應(yīng)用
2.1 常量和基本屬性
| # | 項(xiàng)目 | 描述 |
|---|---|---|
| 1 | ‘xxx’和”xxx” | 創(chuàng)建str,單字節(jié)或者更長(zhǎng)的字節(jié),存放字符串 |
| 2 | ‘’’xxx’’’和”””xxx””” | 創(chuàng)建str |
| 3 | ‘xxx’和”xxx”前面加b或B | 創(chuàng)建bytes,單字節(jié),存放字符對(duì)應(yīng)原始字節(jié)的整數(shù) |
| 4 | ‘’’xxx’’’和”””xxx”””前加b或B | 創(chuàng)建bytes |
示例
>>> b=b'abc' # 返回每個(gè)字符的原始字節(jié)的整數(shù)
>>> s='abc'
>>> type(b),type(s)
(<class 'bytes'>, <class 'str'>)
>>> b,s
(b'abc', 'abc')
>>> b[0],s[0]
(97, 'a')
>>> b[1:],s[1:]
(b'bc', 'bc')
>>> list(b),list(s)
([97, 98, 99], ['a', 'b', 'c'])
# bytes 和 str 不可修改
>>> b[0]='d'
Traceback (most recent call last):
File "<pyshell#32>", line 1, in <module>
b[0]='d'
TypeError: 'bytes' object does not support item assignment
>>> s[0]='d'
Traceback (most recent call last):
File "<pyshell#33>", line 1, in <module>
s[0]='d'
TypeError: 'str' object does not support item assignment
>>> c=b'''
abc
def
'''
>>> c
b'\nabc\ndef\n'
2.2 轉(zhuǎn)換
| # | 項(xiàng)目 | 描述 |
|---|---|---|
| 1 | str.encode()和 bytes(s,encoding) | 編碼,把字符串轉(zhuǎn)換為bytes形式,根據(jù)str創(chuàng)建一個(gè)bytes。 |
| 2 | bytes.decode()和 str(b,encoding) | 解碼,把bytes轉(zhuǎn)換為字符串形式,根據(jù)bytes創(chuàng)建一個(gè)str。 |
| 3 | sys.getdefaultencoding() | 查看系統(tǒng)默認(rèn)編碼 |
描述
str.encode(encoding),根據(jù)編碼名encoding將字符串str編碼為原始字節(jié),encoding默認(rèn)為utf-8。
bytes(s,encoding),根據(jù)編碼名encoding將字符串s編碼為原始字節(jié),encoding必填。
bytes.decode(encoding),根據(jù)編碼名encoding將原始字節(jié)解碼為字符串,encoding默認(rèn)為utf-8。
str(b,encoding),根據(jù)編碼名encoding將原始字節(jié)解碼為字符串,encoding不填則打印原始字節(jié)。
示例
>>> import sys
# 查看系統(tǒng)
>>> sys.platform
'win32'
# 查看系統(tǒng)默認(rèn)編碼
>>> sys.getdefaultencoding()
'utf-8'
>>> s='梯'
# encode 和 bytes 編碼,字符串轉(zhuǎn)原始字節(jié)
>>> s.encode()# encoding 默認(rèn)為 utf-8
b'\xe6\xa2\xaf'
>>> s.encode(encoding='utf-8')
b'\xe6\xa2\xaf'
>>> s.encode(encoding='gbk')
b'\xcc\xdd'
>>> bytes(s,encoding='gbk')
b'\xcc\xdd'
>>> bytes(s,encoding='utf-8')
b'\xe6\xa2\xaf'
# decode 和 str 解碼,原始字節(jié)轉(zhuǎn)字符串
>>> b=b'\xe6\xa2\xaf'
>>> b.decode()
'梯'
>>> str(b,encoding='utf-8')
'梯'
# str() 沒(méi)有 encoding ,則 進(jìn)行打印
>>> str(b)
"b'\\xe6\\xa2\\xaf'"
# bytes() 沒(méi)有 encoding ,則 報(bào)錯(cuò)
>>> bytes(s)
Traceback (most recent call last):
File "<pyshell#62>", line 1, in <module>
bytes(s)
TypeError: string argument without an encoding到此這篇關(guān)于python字符串編碼解碼的使用的文章就介紹到這了,更多相關(guān)python字符串編碼解碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python對(duì)ElasticSearch獲取數(shù)據(jù)及操作
這篇文章主要為大家詳細(xì)介紹了Python對(duì)ElasticSearch獲取數(shù)據(jù)及操作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04
基于Python實(shí)現(xiàn)股票數(shù)據(jù)分析的可視化
在購(gòu)買股票的時(shí)候,可以使用歷史數(shù)據(jù)來(lái)對(duì)當(dāng)前的股票的走勢(shì)進(jìn)行預(yù)測(cè),這就需要對(duì)股票的數(shù)據(jù)進(jìn)行獲取并且進(jìn)行一定的分析。本文將介紹如何通過(guò)Python實(shí)現(xiàn)股票數(shù)據(jù)分析的可視化,需要的可以參考一下2021-12-12
python使用Queue在多個(gè)子進(jìn)程間交換數(shù)據(jù)的方法
這篇文章主要介紹了python使用Queue在多個(gè)子進(jìn)程間交換數(shù)據(jù)的方法,實(shí)例分析了Queue實(shí)現(xiàn)進(jìn)程間數(shù)據(jù)交互的技巧,需要的朋友可以參考下2015-04-04
Python框架Flask的基本數(shù)據(jù)庫(kù)操作方法分析
這篇文章主要介紹了Python框架Flask的基本數(shù)據(jù)庫(kù)操作方法,結(jié)合實(shí)例形式分析了Flask框架數(shù)據(jù)庫(kù)操作常用函數(shù)功能、用法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2018-07-07
scrapy數(shù)據(jù)存儲(chǔ)在mysql數(shù)據(jù)庫(kù)的兩種方式(同步和異步)
這篇文章主要介紹了scrapy數(shù)據(jù)存儲(chǔ)在mysql數(shù)據(jù)庫(kù)的兩種方式(同步和異步),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
Python實(shí)現(xiàn)仿真雙徑效應(yīng)的方法
雙徑模型是一種很好的近似,能夠準(zhǔn)確地反映信號(hào)的傳播特性。這篇文章主要介紹了Python實(shí)現(xiàn)仿真雙徑效應(yīng)的方法,感興趣的小伙伴們可以參考一下2021-05-05

