#coding=utf-8
x={'123':"我"}
print x['123']
print x['123'].decode("utf-8")
name=str(x).split("'")[3]
print name
print name.decode("utf-8")
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
x = {'123': "我"}此时utf8编码下,x为
{'123': '\xe6\x88\x91'}x是字典,作
str(x)得到"{'123': '\\xe6\\x88\\x91'}"str(x).split("'")得到['{', '123', ': ', '\\xe6\\x88\\x91', '}']python解释器允许交互式打印,过程中可以清楚看见字典
__str__()方法的实现x['123'] = x['123'].decode("utf-8")
Solution 1
Solution 2
Solution2,
1. 把name stringfy之後變成
'\\xe6\\x88\\x91'2. 移除
\x變成'e68891'3. decode成為hex
4. 再變成'utf-8'
5. print出來.