>> a = '\\u4fee\\u6539\\u8">
输入banner图图片脚本导航/分类

pythonencode和decode的妙用

字符串解码:

有个字符串:

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

想转换为:

u"\u4fee\u6539\u8282\u70b9\u72b6\u6001\u6210\u529f"

可以通过如下

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

>>> a.decode('unicode_escape')

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

字符串转换为16进制:

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

'68656c6c6f'


相应的还可以


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

'hello'


可以检查下手册,看看哪些codec可用