Home  >  Article  >  Backend Development  >  The wonderful uses of python encode and decode

The wonderful uses of python encode and decode

高洛峰
高洛峰Original
2016-10-19 16:01:121773browse

String decoding:

There is a string:

'\u4fee\u6539\u8282\u70b9\u72b6\u6001\u6210\u529f'

Want to convert to:

u"u4feeu6539u8282u70b9u72b6u 6001u6210u529f"

can be passed as follows

>> a = '\u4fee\u6539\u8282\u70b9\u72b6\u6001\u6210\u529f'

>>> a.decode('unicode_escape')

u'u4feeu6539u8282u70b9u72b6u6001u621 0u529f'

string converted to 16 Base:

>>> "hello".encode("hex")

'68656c6c6f'


The corresponding one is OK


>>> '68656c6c6f'.decode("hex")

'hello'


You can check the manual to see which codecs are available


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn