Python3 JSON file operation

Source: Internet
Author: User

JSON functions

Before using JSON functions, you first need to import the JSON module, the import JSON

1), Json.dumps () function

This function encodes a Python object into a JSON string, for example:

1 ImportJSON2d={'Qian':{'Sex':'male','Addr':'Beijing',' Age': 34},'Chin':{'Sex':'female','Addr':'Beijing',' Age': 34},}                  3 Print(Json.dumps (d,ensure_ascii=false,indent=4))#The dictionary turns into JSON, the dictionary is converted to a string plus ensure_ascii=false, the Chinese is recognized, and the indent=4 is 4 spaces apart4 5 #The result of the above code conversion is:6 {7     "Qian": {8         "Sex":"male",9         "Addr":"Beijing",Ten         " Age": 34 One     }, A     "Chin": { -         "Sex":"female", -         "Addr":"Beijing", the         " Age": 34 -     } -}

2), Json.dump () function

This function allows you to encode a Python object into a JSON string and write it to the file yourself, without having to write the file separately, for example:

1 ImportJSON2d={'Qian':{'Sex':'male','Addr':'Beijing',' Age': 34},'Chin':{'Sex':'female','Addr':'Beijing',' Age': 34},}  3FW =open ('User_info.json','W', encoding='Utf-8')#open an empty file named ' User_info.json '4Json.dump (d,fw,ensure_ascii=false,indent=4)#The dictionary turns into JSON, the dictionary is converted to a string, and you do not need to write the file, you write the JSON string to the ' User_info.json ' file .

3), json.loads () function

The original contents of the document Product.json are as follows:

{    "iphone":{        "Color":"Red",        "Num": 1,        " Price": 98.5    },    "Wather":{        "Num": 100,        " Price": 1,        "Color":" White"    }}

Use the Json.loads () function to turn the JSON string into a Python data type:

1 ImportJSON2F =open ('Product.json', encoding='Utf-8')#Open the JSON file for ' Product.json '3Res=f.read ()#Read the file4 Print(Json.loads (RES))#change the JSON string to Python's data type: Dictionary5 6 #the results of the above conversions are:7{'iphone': {'Color':'Red','Num': 1,' Price': 98.5},'Wather': {'Num': 100,' Price': 1,'Color':' White'}}

4), Json.load () function

This function turns the JSON string into a Python data type: A dictionary, a file object, which will help you read the file, no need to read the file separately, for example:

ImportJSON F=open ('Product.json', encoding='Utf-8')#Open FilePrint(Json.load (f))#Turn the JSON string into Python data type: Dictionary, pass a file object, it will help you read the file, do not need to read the file separately#The result of the above conversion execution is:{'iphone': {'Color':'Red','Num': 1,' Price': 98.5},'Wather': {'Num': 100,' Price': 1,'Color':' White'}}

Python3 JSON file operation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.