当前位置:天才代写 > tutorial > Python教程 > python encode和decode的妙用

python encode和decode的妙用

2017-11-02 08:00 星期四 所属: Python教程 浏览:577

字符串解码:

有个字符串:

'\\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可用

 

    关键字:

天才代写-代写联系方式