Python shelve module

Source: Internet
Author: User

Import Shelvedef createdata ():    try:        db = Shelve.open (' shelve_test ')        # Key with value must be string        db[' int '] = 1        db[' float '] = 2.3        db[' string '] = "I like python."        db[' key ' = ' value '    finally:        db.close () def loaddata ():                       #这个可以另外写一个. py file to read    db = Shelve.open (' Shelve _test ') for    item in Db.items (): Print (        item)    Print (db[' int ')) print (    db[' float '))    print (db [' String '])    Print (db[' key '))    db.close () if __name__ = = ' __main__ ':    createdata ()    loaddata ()

The shelve module is simpler than the Pickle module, with only one open function, which returns a dictionary-like object, readable and writable

Pickle write: Open a file descriptor in write mode, call Pickle.dump to write the object in

Import Pickledn = {' Baidu ': ' www.baidu.com ', ' qq ': ' www.qq.com ', ' a ': ' www.360.cn '}name = [' Mayun ', ' Mahuateng ', ' Liyanhong ']f = open (' A.txt ', ' WB ')  # ' WB ' is written in binary pickle.dump (dn,f)      # #写一个对象pickle. Dump (name,f) #  # Write one More object F.close () read: Open file descriptor in read mode, call Pickle.load Load Object f = open (' a.txt ', ' RB ')    # ' WB ' read pickle.load (f)             # # Load an object Pickle.load (f)             # #加载另一个对象f. Close ()

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.