python encode和decode的妙用
更新時間:2009年09月02日 01:27:08 作者:
做網(wǎng)絡(luò)編程的時候,經(jīng)常需要把接收到的數(shù)據(jù)用16進(jìn)制的方式打印出來,方便查看。今天發(fā)現(xiàn)在Python下有這樣一個簡單的方法。
>>> "hello".encode("hex")
'68656c6c6f'
相應(yīng)的還可以
>>> '68656c6c6f'.decode("hex")
'hello'
查了一下手冊,還有這些codec可用
|
Codec |
Aliases |
Operand type |
Purpose |
| base64_codec | base64, base-64 | byte string | Convert operand to MIME base64 |
| bz2_codec | bz2 | byte string | Compress the operand using bz2 |
| hex_codec | hex | byte string | Convert operand to hexadecimal representation, with two digits per byte |
| idna | Unicode string | Implements RFC 3490. New in version 2.3. See also encodings.idna | |
| mbcs | dbcs | Unicode string | Windows only: Encode operand according to the ANSI codepage (CP_ACP) |
| palmos | Unicode string | Encoding of PalmOS 3.5 | |
| punycode | Unicode string | Implements RFC 3492. New in version 2.3. | |
| quopri_codec | quopri, quoted-printable, quotedprintable | byte string | Convert operand to MIME quoted printable |
| raw_unicode_escape | Unicode string | Produce a string that is suitable as raw Unicode literal in python source code | |
| rot_13 | rot13 | Unicode string | Returns the Caesar-cypher encryption of the operand |
| string_escape | byte string | Produce a string that is suitable as string literal in python source code | |
| undefined | any | Raise an exception for all conversions. Can be used as the system encoding if no automatic coercion between byte and Unicode strings is desired. | |
| unicode_escape | Unicode string | Produce a string that is suitable as Unicode literal in python source code | |
| unicode_internal | Unicode string | Return the internal representation of the operand | |
| uu_codec | uu | byte string | Convert the operand using uuencode |
| zlib_codec | zip, zlib | byte string | Compress the operand using gzip |
相關(guān)文章
對python特殊函數(shù) __call__()的使用詳解
今天小編就為大家分享一篇對python特殊函數(shù) __call__()的使用詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07
python pandas.DataFrame選取、修改數(shù)據(jù)最好用.loc,.iloc,.ix實(shí)現(xiàn)
今天小編就為大家分享一篇python pandas.DataFrame選取、修改數(shù)據(jù)最好用.loc,.iloc,.ix實(shí)現(xiàn)。具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06
使用 Python 處理 JSON 格式的數(shù)據(jù)
JSON 是一個很好的選擇。如果你對 Python 有所了解,就更加事半功倍了。下面就來介紹一下如何使用 Python 處理 JSON 數(shù)據(jù)。感興趣的朋友跟隨小編一起看看吧2019-07-07
思考分析Python運(yùn)算中?a+=b?和?a=a+b是否相等
這篇文章主要為大家介紹了Python運(yùn)算中a+=b和a=a+b是否相等及原理思考分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
詳細(xì)聊聊為什么Python中0.2+0.1不等于0.3
最近在學(xué)習(xí)過程中發(fā)現(xiàn)在計(jì)算機(jī)JS時發(fā)現(xiàn)了一個非常有意思事,0.1+0.2的結(jié)果不是0.3,而是0.30000000000000004,下面這篇文章主要給大家介紹了關(guān)于為什么Python中0.2+0.1不等于0.3的相關(guān)資料,需要的朋友可以參考下2022-12-12

