程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Basic usage of JSON Library of Python standard library module

編輯:Python

Catalog

Preface

effect

loads,load Usage of

dumps,dump Usage of

Conclusion

Preface

json, Its full name is JavaScript Object Notation, That is to say JavaScript Object tag , Data is represented by a combination of objects and arrays , Although the structure is simple, the degree of structure is very high , Is a lightweight data exchange format .

effect

Mainly used to python Object code is json Format output or storage , And will be json Format object decoded as python object .

One JSON Objects can be written in the following form :

[{ "name": " Xiao Ming ", "height": "170", "age": "18"}, { "name": " Xiaohong ", "height": "165", "age": "20"}]

Surrounded by brackets is equivalent to list type , Each element in the list can be of any type , In this case it's a dictionary type , Surrounded by braces .

JSON It can be freely combined from the above two forms , You can nest... Infinitely , The structure is clear , It's a great way to exchange data .

Let's take a look at json in loads, and load

loads,load Usage of

for example , There is a passage JSON String of form , It is str type , We use it json.loads convert to python Data structure of , Become a list or dictionary , So we can operate .

import jsondata = '''[{ "name": " Xiao Ming ", "height": "170", "age": "18"}, { "name": " Xiaohong ", "height": "165", "age": "20"}]'''# Print data type print(type(data))# json Type of data is converted to python Data of type new_data = json.loads(data)# Print data type print(type(new_data))

The operation results are as follows

In this way, we can use the index to get the corresponding content , For example, you want to get... In the first element name attribute , You can use the following methods :

import jsondata = '''[{ "name": " Xiao Ming ", "height": "170", "age": "18"}, { "name": " Xiaohong ", "height": "165", "age": "20"}]'''# Print data type print(type(data))# json Type of data is converted to python Data of type new_data = json.loads(data)# Get content name = new_data[0]['name']new_name = new_data[0].get('name')# Print after conversion data type print(type(new_data))print(name)print(new_name)

Running results

That's all loads Usage of .

load The usage of is to put json Format file , convert to python Data of type .

Be careful :load Method operates on the entire file object , Here is to convert the contents of the whole file object into json object .( The following figure shows the operation object )

Example

import jsonimport json# load The usage of is to put json Format file , convert to python Data of type .# The file object that builds the file with open('test1.json',encoding='utf-8')as fp: # Load the long file object , Convert to python Data of type pyth_list = json.load(fp) print(pyth_list) print(type(pyth_list)) print(type(pyth_list[0]))

A file operation object running result is required

That's all loads and load Usage of , Both methods can be used in appropriate scenarios .

dumps,dump Usage of

json.dumps() function , hold python Type of data to json character string

json.dump() function , hold python Type of data to json Save format to file

dumps function

import jsondata = '''[{ "name": " Xiao Ming ", "height": "170", "age": "18"}, { "name": " Xiaohong ", "height": "165", "age": "20"}]'''# Print coming data type print(type(data))# json Type of data is converted to python Data of type new_data = json.loads(data)# hold python Type of data to json character string lit = json.dumps(new_data)# Print after conversion data type print(type(new_data))print(type(lit))

Running results

Actually loads and dumps The usage of is consistent ,loads Yes, it will json Type of data is converted to python Data of type , and dumps Yes, it will json Type of data is converted to python Data of type . One is encoding and the other is decoding .

dump function

hold python Type of data to json Save format to file

import jsonimport jsondata = '''[{ "name": " Xiao Ming ", "height": "170", "age": "18"}, { "name": " Xiaohong ", "height": "165", "age": "20"}]'''# json Type of data is converted to python Data of type new_data = json.loads(data)# hold python Type of data to json Save format to file # Build the object to write to the file with open('test1.json','w',encoding='utf-8')as fp: # hold python Type of data to json Save format to file json.dump(new_data,fp,ensure_ascii=False)

In order to output Chinese , You also need to specify parameters ensure_ascii by False

Save files

Conclusion

This is about python Standard library module json This is the end of the article on the basic usage of the library , More about python Standard library module json Please search the previous articles of SDN or continue to browse the related articles below. I hope you will support SDN more in the future !



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved