Python is a simple implementation of reading and writing json files,

Source: Internet
Author: User

Python is a simple implementation of reading and writing json files,

JSON (JavaScript Object Notation) is a lightweight data exchange format. It is based on a subset of ECMAScript. JSON uses a completely language-independent text format, but it also uses a habit similar to the C language family (including C, C ++, Java, JavaScript, Perl, Python, and so on ). These features make JSON an ideal data exchange language. Easy to read and write, and easy to parse and generate by machines (generally used to increase the network transmission rate ).

JSON is composed of list and dict in python.

This is the two modules used for serialization:

  1. Json: used for conversion between string and python Data Types
  2. Pickle: used for conversion between python-specific types and python Data Types

The Json module provides four functions: dumps, dump, loads, and load.

The pickle module provides four functions: dumps, dump, loads, and load.

Json dumps converts the data type to a string dump converts the data type to a string and stores it in the file loads converts the string to the Data Type load converts the file open from the string to the Data Type

Json can exchange data between different languages, while pickle is only used between python. Json can only serialize the most basic data types. josn can only serialize common data types, such as list, Dictionary, list, String, number, and object type! Josn won't work. Pickle can serialize all data types, including classes and functions.

Example:

Dumps: converts a dictionary in python to a string.

Import jsontest_dict = {'bigberg ': [7600, {1: [['iphone', 6300], ['bike', 800], ['shirt ', 300]} print (test_dict) print (type (test_dict) # dumps converts data into a string json_str = json. dumps (test_dict) print (json_str) print (type (json_str ))

Loads: converts a string to a dictionary.

 new_dict = json.loads(json_str) print(new_dict) print(type(new_dict))

Dump: write data into a json File

With open (".. /config/record. json "," w ") as f: json. dump (new_dict, f) print ("loading file completed... ")

Load: open the file and convert the string to the data type.

with open("../config/record.json",'r') as load_f:  load_dict = json.load(load_f)  print(load_dict)load_dict['smallberg'] = [8200,{1:[['Python',81],['shirt',300]]}]print(load_dict)with open("../config/record.json","w") as dump_f:  json.dump(load_dict,dump_f)

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

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.