基本特点
tuple
==
list
==
dictionary
==
遍历
tuple
==
list
==
dictionary
==
转换
str==>dictionary
使用ast
MAC OSX 10.9.2
>>> en = "China".decode('utf8')
>>> cn = "中国".decode('utf8')
>>> print en
China
>>> print cn
中国
>>> s1 = {'en': en, 'cn': cn}
>>> print s1
{'en': u'China', 'cn': u'\u4e2d\u56fd'}
>>> s2 = "{'en': u'China', 'cn': u'\\u4e2d\\u56fd'}"
>>> ast.literal_eval(s2)
{'en': u'China', 'cn': u'\u4e2d\u56fd'}
>>> print ast.literal_eval(s2) #把字符串s2转换成dictionary
{'en': u'China', 'cn': u'\u4e2d\u56fd'}
json string ==> dictionary
使用json.loads 要求字符串符合 json格式
1
Windows7
>>> ss1 = '[{"name":"sam","cn":"山姆"}]'
>>> json.loads(ss1)
[{u'name': u'sam', u'cn': u'\u027d\u0137'}]
2
Windows7
>>> ss = '{"one": null,"two":{"a": "1", "b": null, "c": "西"}, "three": "3"}'
>>>
>>> sj = json.loads(ss.decode('gbk'))
>>> print sj
{u'three': u'3', u'two': {u'a': u'1', u'c': u'\u897f', u'b': None}, u'one': None
}
>>> sj["two"]["c"]
u'\u897f'
>>> print sj["two"]["c"]
西
>>>
3Windows7
>>> ss = '[{"one": null,"two":{"a": "1", "b": null, "c": "西"}, "three": "3"}, {
"oo": "欧", "tt": "22"}]'
>>> sj = json.loads(ss.decode('gbk'))
>>> sj[1]["oo"]
u'\u6b27'
>>> print sj[1]["oo"]
欧
>>>