How to solve python dict garbled

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

How to solve python dict garbled
How to comment python code
How to learn python quickly
How to uninstall python plugin
How to understand python objects
How to use python tuples
Python| function using recursion to solve
python how to view webpage code
How to use python thread pool
How to write python configuration file
How to save the python program
How to omit parentheses in Python
How to install Python 3.8 on CentOS 8
How to write classes in python
How to filter numbers in python
How to read Excel in Python
How to install Python on CentOS 8
How to view errors in python
How to write return in python
How to view the python module
How to understand variables in Python
How to clear variables in python
How to understand python object-oriented programming
How to use SQLite in Python
How to verify successful installation of python
How to make a globe with Python
How to use and and or in Python
How to delete cache files in python
How to represent null values in python
How to save text files in python
How to use PYTHON to crawl news articles
How to write win programs in python
How to run id function in python
How to custom catch errors in python
How to write try statement in python
How to define private attributes in Python
R&D: How To Install Python 3 on CentOS 7
How to add custom modules in Python
How to process excel table with python
How to understand global variables in Python
How to view installed modules in python
How to install Python2 on Ubuntu20.04 ubuntu/focal64
How to debug python program using repr
How to learn the Python time module
How to open python in different systems
How to sort a dictionary in python
How to get started quickly with Python
How to enter python triple quotation marks
How to add background music in python
How to represent relative path in python
linux+ubuntu solve python
01. Introduction to Python
Introduction to Python
How to upgrade all Python libraries on Ubuntu 18.04
How to use the round function in python
How to convert web pages to PDF with Python
How to use the zip function in Python
How to install python in ubuntu server environment
How to simulate gravity in a Python game
How to use the format function in python
How to play happily with Python3 on Ubuntu