python字符串編碼解碼的使用
python通過ord©獲取字符c的unicode的編碼值,為整數(shù)。通過chr(i)獲取i對應的unicode的字符。通過str.encode()將字符串編碼為原始字節(jié),b.decode()將原始字節(jié)解碼為字符串。
1 字符串基礎知識
python通過ord©獲取字符c的unicode的編碼值,為整數(shù)。
通過chr(i)獲取i對應的unicode的字符。
1.1 字符編碼方法
ASCII碼定義0-127的字符代碼,每個字符存儲在一個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位文本和二進制數(shù)據(jù) |
| 2 | unicode表示寬字符unicode文本 | |
| 3 | py3.x | str表示unicode文本 |
| 4 | bytes表示二進制數(shù)據(jù) | |
| 5 | bytearray,可變的bytes類型 |
1.3 文本和二進制文件
| # | 模式 | 描述 |
|---|---|---|
| 1 | bytes和二進制模式 | 處理圖像文件、解壓的打包數(shù)據(jù)、設備數(shù)據(jù)流 |
| 2 | str和文本模式 | 用于程序輸出、HTML、CSV、XML文件 |
2 python3.0的字符串應用
2.1 常量和基本屬性
| # | 項目 | 描述 |
|---|---|---|
| 1 | ‘xxx’和”xxx” | 創(chuàng)建str,單字節(jié)或者更長的字節(jié),存放字符串 |
| 2 | ‘’’xxx’’’和”””xxx””” | 創(chuàng)建str |
| 3 | ‘xxx’和”xxx”前面加b或B | 創(chuàng)建bytes,單字節(jié),存放字符對應原始字節(jié)的整數(shù) |
| 4 | ‘’’xxx’’’和”””xxx”””前加b或B | 創(chuàng)建bytes |
示例
>>> b=b'abc' # 返回每個字符的原始字節(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)換
| # | 項目 | 描述 |
|---|---|---|
| 1 | str.encode()和 bytes(s,encoding) | 編碼,把字符串轉(zhuǎn)換為bytes形式,根據(jù)str創(chuàng)建一個bytes。 |
| 2 | bytes.decode()和 str(b,encoding) | 解碼,把bytes轉(zhuǎn)換為字符串形式,根據(jù)bytes創(chuàng)建一個str。 |
| 3 | sys.getdefaultencoding() | 查看系統(tǒng)默認編碼 |
描述
str.encode(encoding),根據(jù)編碼名encoding將字符串str編碼為原始字節(jié),encoding默認為utf-8。
bytes(s,encoding),根據(jù)編碼名encoding將字符串s編碼為原始字節(jié),encoding必填。
bytes.decode(encoding),根據(jù)編碼名encoding將原始字節(jié)解碼為字符串,encoding默認為utf-8。
str(b,encoding),根據(jù)編碼名encoding將原始字節(jié)解碼為字符串,encoding不填則打印原始字節(jié)。
示例
>>> import sys
# 查看系統(tǒng)
>>> sys.platform
'win32'
# 查看系統(tǒng)默認編碼
>>> sys.getdefaultencoding()
'utf-8'
>>> s='梯'
# encode 和 bytes 編碼,字符串轉(zhuǎn)原始字節(jié)
>>> s.encode()# encoding 默認為 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() 沒有 encoding ,則 進行打印
>>> str(b)
"b'\\xe6\\xa2\\xaf'"
# bytes() 沒有 encoding ,則 報錯
>>> 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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python對ElasticSearch獲取數(shù)據(jù)及操作
這篇文章主要為大家詳細介紹了Python對ElasticSearch獲取數(shù)據(jù)及操作,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-04-04
基于Python實現(xiàn)股票數(shù)據(jù)分析的可視化
在購買股票的時候,可以使用歷史數(shù)據(jù)來對當前的股票的走勢進行預測,這就需要對股票的數(shù)據(jù)進行獲取并且進行一定的分析。本文將介紹如何通過Python實現(xiàn)股票數(shù)據(jù)分析的可視化,需要的可以參考一下2021-12-12
python使用Queue在多個子進程間交換數(shù)據(jù)的方法
這篇文章主要介紹了python使用Queue在多個子進程間交換數(shù)據(jù)的方法,實例分析了Queue實現(xiàn)進程間數(shù)據(jù)交互的技巧,需要的朋友可以參考下2015-04-04
Python框架Flask的基本數(shù)據(jù)庫操作方法分析
這篇文章主要介紹了Python框架Flask的基本數(shù)據(jù)庫操作方法,結(jié)合實例形式分析了Flask框架數(shù)據(jù)庫操作常用函數(shù)功能、用法及相關(guān)注意事項,需要的朋友可以參考下2018-07-07
scrapy數(shù)據(jù)存儲在mysql數(shù)據(jù)庫的兩種方式(同步和異步)
這篇文章主要介紹了scrapy數(shù)據(jù)存儲在mysql數(shù)據(jù)庫的兩種方式(同步和異步),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-02-02

