How to read and write JSON files using Python

Source: Internet
Author: User
JSON (JavaScript Object Notation) is a lightweight data interchange format. It is based on a subset of ECMAScript. JSON takes a completely language-independent text format, but also uses a similar idiom to the C language family (c, C + +, Java, JavaScript, Perl, Python, and so on). These features make JSON an ideal data exchange language. Easy to read and write, but also easy to machine parse and generate (typically used to improve network transfer rates).

JSON is made up of lists and dict, respectively, in Python.

This is the two modules used for serialization:

    • JSON: Used to convert between string and Python data types

    • Pickle: Converting between Python-specific types and Python data types

The JSON module provides four functions: dumps, dump, loads, load

The Pickle module provides four functions: dumps, dump, loads, load

The JSON dumps converts the data type to a string dump to convert the data type into a string and store it in a file loads convert the string to a data type load opens the file to a data type from a string

JSON is the ability to exchange data between different languages, and pickle is used only between Python. JSON can only serialize the most basic data types, JOSN can only serialize the commonly used data types (list, dictionary, list, string, number,), such as date format, class Object! Josn will die. Pickle can serialize all data types, including classes, and functions can be serialized.

Case:

Dumps: Converting a dictionary from Python to a string

Import jsontest_dict = {' Bigberg ': [7600, {1: [' IPhone ', 6300], [' Bike ', ' + ', ' shirt ', 300]]}]}print (test_dict) print (t Ype (test_dict)) #dumps convert data to String json_str = Json.dumps (test_dict) print (JSON_STR) print (Type (JSON_STR))

Loads: Converting a string to a dictionary

New_dict = Json.loads (json_str) print (new_dict) print (Type (new_dict))

Dump: Writing data to a JSON file

With open (".. /config/record.json "," W ") as F:json.dump (new_dict,f) print (" Load in File Complete ... ")

Load: Opens the file and transforms the string into a 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)

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.