Python Common Modules 2

Source: Internet
Author: User

Python Common module Two

Shelve
Pickle
Json

Shelve module
    • The internal structure is actually the dictionary type, the storage time specifies the corresponding key, the content is stored as value, and takes out the value directly through key.
    • Note that the file name does not require any suffixes because the system defaults to. db as the suffix name.

    • Code 1 storing content

import shelvedef fun1():    pass= shelve.open(‘test_shelve‘)s[‘dic‘= {}.fromkeys(list(‘abcdefg‘0)s[‘func‘= fun1s[‘list‘=list(range(10))s.close()
    • Code 2 removing content
= shelve.open(‘test_shelve‘= shelve.open(‘test_shelve‘= s.get(‘dic‘= s.get(‘func‘= s.get(‘list‘)
Pickle Module
    • Writes and reads are read as bytes, and encoding and decoding formats can be specified

    • Write file

import=open(‘save.pkl‘‘wb‘, encoding=‘utf8‘= {}.fromkeys(list(‘qwertasd‘1)pickle.dump(dic, f)f.close()
    • Read file
import=open(‘save.pkl‘‘rb‘= pickle.load(f)printtype(data))
JSON module
    • Modules include the following functions:

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

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

    • Code Listing 1: Storing content in JSON serialized format file

= [‘我‘‘你‘‘它‘‘asdas‘=open(‘save.json‘‘w‘)json.dump(s, f)f.close()
    • Code Listing 2: Using JSON modules to read JSON-formatted content
=open(‘save.json‘‘r‘, encoding=‘utf8‘= json.load(f1)# result = json.loads(f1.read())
    • Serializes the string JSON into a file that is encoded in Unicode format
= json.dumps(s)printtype(data))# ["\u6211", "\u4f60", "\u5b83"] <class ‘str‘>=# 直接反序列json序列化数据print(result)# [‘我‘, ‘你‘, ‘它‘]
JSON, Pickle, shelve three differences

The main differences are several points:

    1. The pickle and shelve modules support all Python data types, including functions, but JSON only supports data types such as Dic/list/tuple/str/int;
    2. Pikle and shelve can only be used in Python, but JSON is cross-lingual;
The role of JSON, Pickle, shelve three modules
    • For converting between strings and python or other programming language data types, not only can you save data to your hard disk, but you can also save its data type

Python Common Modules 2

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.