Python shelve module

Source: Internet
Author: User

I. Introduction in PYTHON3 we use JSON or pickle persisted data, can dump multiple times, but only load once, because the previous data has been overwritten by the data behind the dump. If we want to implement dump and load multiple times, we can use the shelve module. The shelve module can persist all data types supported by the pickle.
Code:

#持久化数据import Shelveimport datetime# info = {' name ': ' Bigberg ', ' age ': 22}# name = [' Apoll ', ' zous ', ' Luna ']# t = DateTime. DateTime.Now () # # with Shelve.open (' Shelve.txt ') as f:#     f[' name '] = name  # Persistent List #     f[' info '] = info  # Persistent Dictionary #     f[' time ' = t # persistence-Type # # #  # # # # # # # # # # # # # # # # # 3 Files Generated: Shelve.txt.bak, Shelve.txt.dat, Shelve.txt.dir. #删除 # with Shelve.open (' Shelve.txt ') as f:#     del f[' name '] #获取数据: Use Getimport shelve# with Shelve.open (' Shelve.txt ') as F : #     n = f.get (' name ') #     i = f.get (' info ') # now     = F.get (' time ') # # print (n) # print (i) # print (now) # output
Summarize:

1, shelve module is a simple key,value memory data through the file persistence module. 2. The shelve module can persist any Python data format that pickle can support. 3, shelve is a package of pickle module. 4, shelve module can be multiple dump and load.

Python shelve module

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.