日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

关于 Python3 的编码

發(fā)布時(shí)間:2025/7/14 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 关于 Python3 的编码 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
  • Python3 中 str 與 bytes 的轉(zhuǎn)換:The bytes/str dichotomy in Python 3

  • 字符與 Unicode 編號(hào)之間的轉(zhuǎn)換

#?字符轉(zhuǎn)?Unicode?編號(hào) >>>?ord('A') 65 >>>?hex(ord('A')) '0x41'>>>?ord('你') 20320 >>>?hex(ord('你')) '0x4f60'>>>?'你好'.encode('unicode_escape') b'\\u4f60\\u597d'#?Unicode?編號(hào)轉(zhuǎn)字符 >>>?chr(65) 'A' >>>?chr(0x41) 'A'>>>?chr(20320) '你' >>>?chr(0x4f60) '你'>>>?b'\\u4f60\\u597d'.decode(('unicode_escape')) '你好' >>>?print(b'\\u4f60\\u597d') b'\\u4f60\\u597d' >>>?print(u'\u4f60\u597d') 你好
  • 漢字與 gbk 十六進(jìn)制(二進(jìn)制)之間的轉(zhuǎn)換。

#?漢字轉(zhuǎn)十六進(jìn)制 >>>?'你好'.encode('gbk') b'\xc4\xe3\xba\xc3'#?十六進(jìn)制轉(zhuǎn)漢字 >>>?b'\xc4\xe3\xba\xc3'.decode('gbk') '你好'#?漢字轉(zhuǎn)十六進(jìn)制字符串 >>>?''.join(?[?'%02X'?%?x?for?x?in?'你好'.encode('gbk')?]?).strip() 'C4E3BAC3' >>>?'?'.join(?[?'%02X'?%?x?for?x?in?'你好'.encode('gbk')?]?).strip() 'C4?E3?BA?C3'#?十六進(jìn)制字符串轉(zhuǎn)漢字 >>>?bytes.fromhex('C4E3BAC3').decode('gbk') '你好' >>>?bytes.fromhex('C4?E3?BA?C3').decode('gbk') '你好'
  • 數(shù)字與十六進(jìn)制(二進(jìn)制)之間的轉(zhuǎn)換

#?數(shù)字轉(zhuǎn)十六進(jìn)制字符串 >>>?hex(21) '0x15' >>>?hex(21)[2:] '15'#?十六進(jìn)制字符串轉(zhuǎn)數(shù)字 >>>?int('0x15',?16) 21 >>>?int('15',?16) 21#?數(shù)字轉(zhuǎn)八進(jìn)制字符串 >>>?oct(21) '0o25' >>>?oct(21)[2:] '25'#?八進(jìn)制字符串轉(zhuǎn)數(shù)字 >>>?int('0o25',?8) 21 >>>?int('25',?8) 21#?數(shù)字轉(zhuǎn)二進(jìn)制字符串 >>>?bin(5) '0b101' >>>?bin(5)[2:] '101'#?二進(jìn)制字符串轉(zhuǎn)數(shù)字 >>>?int('0b101',?2) 5>>>?int('101',?2) 5
  • bytes 相關(guān)

#?bytes?對(duì)象轉(zhuǎn)十六進(jìn)制字符串 >>>?"%02X"?%?ord(b'\xff') 'FF' >>>??''.join(["%02X"?%?i?for?i?in?b'\xe4\xbd\xa0\xe5\xa5\xbd']) 'E4BDA0E5A5BD'#?bytes?轉(zhuǎn)?int >>>?ord(b'\xff') 255#?int?轉(zhuǎn)?bytes >>>?bytes([255]) b'\xff'
  • bit 相關(guān)(需使用第三方包 bitarray)

#?字符串轉(zhuǎn)?01?串(默認(rèn)?endian?是大端) >>>?arr?=?bitarray() >>>?arr.frombytes('你好'.encode('utf8')) >>>?arr.to01() '111001001011110110100000111001011010010110111101'#?01?串轉(zhuǎn)字符串 >>>?bitarray('111001001011110110100000111001011010010110111101').tobytes().decode('utf8') '你好'
  • 關(guān)于utf8的bom頭。(Python3下)

>>>?import?codecs >>>?codecs.BOM_UTF8 b'\xef\xbb\xbf' >>>?len(b'\xef\xbb\xbf') 3 >>>?codecs.BOM_UTF8.decode('utf8') '\ufeff' >>>?len('\ufeff') 1
  • Python3 有哪些編碼:Standard Encodings、Python Specific Encodings?。

  • 打印編碼及別名。(Get a list of all the encodings Python can encode to)

>>>?from?encodings.aliases?import?aliases >>>?for?k?in?aliases:print('%s:?%s'?%?(k,?aliases[k]))
  • 驗(yàn)證是不是有效編碼。

>>>?import?codecs>>>?codecs.lookup('utf8')????#有效 <codecs.CodecInfo?object?for?encoding?utf-8?at?0x13fb4f50828>>>>?codecs.lookup('utf-;8')????#有效 <codecs.CodecInfo?object?for?encoding?utf-8?at?0x13fb4f50a08>>>>?codecs.lookup('utf88')????#無效 Traceback?(most?recent?call?last):File?"<pyshell#4>",?line?1,?in?<module>codecs.lookup('utf88') LookupError:?unknown?encoding:?utf88
  • 標(biāo)準(zhǔn)化 encoding。

>>>?import?encodings >>>?encodings.normalize_encoding('utf-;8') 'utf_8'

對(duì)應(yīng) C 代碼為:unicodeobject.c 中的?_Py_normalize_encoding 函數(shù)。

  • sys/locale 模塊中與編碼相關(guān)的方法。(Python字符編碼詳解)

import?sys import?locale#?當(dāng)前系統(tǒng)所使用的默認(rèn)字符編碼 >>>?sys.getdefaultencoding() 'utf-8'#?用于轉(zhuǎn)換?Unicode?文件名至系統(tǒng)文件名所使用的編碼 >>>?sys.getfilesystemencoding() 'utf-8'#?獲取默認(rèn)的區(qū)域設(shè)置并返回元組(語言,?編碼) >>>?locale.getdefaultlocale() ('zh_CN',?'cp936')#?返回用戶設(shè)定的文本數(shù)據(jù)編碼 #?文檔提到this?function?only?returns?a?guess >>>?locale.getpreferredencoding() 'cp936'
  • 字符串反轉(zhuǎn)

>>>?line?=?'0123456789' >>>?line[::-1] '9876543210'


【相關(guān)閱讀】

  • Unicode Tips

  • Python3 處理 gb18030 亂碼


*** walker ***


轉(zhuǎn)載于:https://blog.51cto.com/walkerqt/1954215

總結(jié)

以上是生活随笔為你收集整理的关于 Python3 的编码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。