Python makes your printed json data format look good and solves the Chinese garbled problem

Python makes the json data format you print out looks good


   

Preface

   Before, there was a need to print some data in json format, because the amount of data is small, and you can find out where it is when you print it on one line, but today there is something like this:

Insert picture description here
ah, there is
Insert picture description here

no way, just solve it!

One, solve the format problem

In fact, a simple sentence can be done

data = data.json() #data为json格式的数据
print(json.dumps(data,sort_keys=True,indent=4))

indent: should be a non-negative integer, if it is 0 or empty, the data will be displayed in one row; otherwise, it will wrap and display the blank in front according to the number of
indents. sort_keys: sort the data according to the value of keys

The effect after treatment:

Insert picture description here

2. Solve the problem of garbled Chinese characters

At this time, some readers will find, huh? Why is there no Chinese anymore?
Good guy, all the Chinese characters are garbled.
Insert picture description here
How to solve this? In fact, you only need to add a parameter to dumps. The above code is modified as follows:

data = data.json() #data为json格式的数据
print(json.dumps(data,sort_keys=True,indent=4,ensure_ascii=False))

It's comfortable~

At last

I am very happy to share with everyone again, and the rest is...please praise! ! ! You have all seen this, creation is not easy, leave your precious likes~

Guess you like

Origin blog.csdn.net/weixin_45386875/article/details/114093060