Define the dictionary and output it directly, the output result is garbled in Chinese
d={'name':'lily','age':18,'sex':'Female','no':1121}
print d
Output result:
{ ‘age’: 18, ‘no’: 1121, ‘name’: ‘lily’, ‘sex’: ‘\xe5\xa5\xb3’}
Solution:
d={'name':'lily','age':18,'sex':'Female','no':1121}
print json.dumps(d,encoding='utf-8',ensure_ascii=False)
Output result:
{ “Age”: 18, “no”: 1121, “name”: “lily”, “sex”: “女”}
Content expansion:
Solution to garbled output of list or dictionary in Python
Problem: The list or dictionary in Python contains Chinese strings. If you use print directly, the following results will appear:
# Print dictionary
dict ={'name':'Zhang San'}
print dict
{' name':'\xe5\xbc\xa0\xe4\xb8\x89'}
# Print list
list =[{'name':'Zhang San'}]
print list
[{' name':'\xe5\xbc\xa0\xe4\xb8\x89'}]
**solution: **
Use the following methods for output:
import json
# Print dictionary
dict ={'name':'Zhang San'}
print json.dumps(dict, encoding="UTF-8", ensure_ascii=False){'name':'Zhang San'}
# Print list
list =[{'name':'Zhang San'}]
print json.dumps(list, encoding="UTF-8", ensure_ascii=False)[{'name':'Zhang San'}]
So far, this article on how to solve the python dict garbled is introduced here. For more related python dict garbled solutions, please search for the previous articles of ZaLou.Cn or continue to browse the related articles below. I hope everyone will support ZaLou.Cn in the future. !
Recommended Posts