How to deal with JSON files in python
This article will explain in detail how to deal with JSON files in python. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
1. Environment
Python3.8
Pycharm 2020.1
two。 Read JSON data
Of course, the first step is to import the json module.
Import json
Read json data
Data ='{"a": 123, "b": "lala"} 'result = json.loads (data) print (result) print (result [' a'])
Interactive interface
3. Write out JSON data
Import module
Import json
Output json data and convert python dictionary to json data
Data = {'a 'axia1234'} result = json.dumps (data) print (result) insert a picture description example here to capture the data of China Seismological Network and parse the JSON data packet.
Complete source code
Import jsonimport requests
Res = requests.get ('http://news.ceic.ac.cn/ajax/google')text = res.text.encode (' utf-8') result = json.loads (text))
Print ('%-20s'% id','%-20s'% place','%-20s'% magnitude','%-20s'% longitude','%-20s'% latitude','%-20s'% depth','%-20s'% time') for e in result: print ('%-20s'% str (e ['id']) '%-20s'% str (e [' LOCATION_C']),'%-20s'% str (e ['M']),'%-20s'% str (e ['EPI_LON']),'%-20s'% str (e ['EPI_LAT']),'%-20s'% str (e ['EPI_DEPTH']),'%-20s'% str (e ['EPI_DEPTH']])
Typesetting is a bit lousy, everyone forgive me.
On how to deal with JSON files in python to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.