解决python输出字典,列表含中文乱码问题
一、定义字典和列表并直接输出,结果输出结果中文是乱码展示
d={'name':'lily','age':18,'sex':'女','no':1121}
e=['你好',1,'apple']
print d
print e
输出结果:
{'age': 18, 'no': 1121, 'name': 'lily', 'sex': '\xe5\xa5\xb3'}
['\xe4\xbd\xa0\xe5\xa5\xbd', 1, 'apple']
二、解决办法:
d={'name':'lily','age':18,'sex':'女','no':1121}
e=['你好',1,'apple']
print json.dumps(d,encoding='utf-8',ensure_ascii=False)
print json.dumps(e,encoding='utf-8',ensure_ascii=False)
输出结果:
{"age": 18, "no": 1121, "name": "lily", "sex": "女"}
["你好", 1, "apple"]