相信很多人都被python各种类型转换烦得要死,本文将提供一些思路:
1.unicode,str互转:
>>> temp = u"hello world"
>>> type(temp)
<type 'unicode'>
1)unicode--->str:
>>> temp = temp.encode("unicode_escape").decode("string_escape")
>>> type(temp)
<type 'str'>
2)str--->unicode:
json.dumps()
2.格式为dict类型的str转化为dict:
json.loads()
本文深入解析Python中unicode与str的互相转换方法,以及如何将格式为dict类型的字符串转化为真正的字典对象。针对unicode到str的转换,提供了使用encode与decode函数的具体步骤;对于str到unicode的转换,推荐使用json.dumps()函数;最后介绍了如何利用json.loads()函数将字符串形式的字典转换为Python的字典类型。
4851

被折叠的 条评论
为什么被折叠?



