Saving a Python dict to a file using pickle
2017-07-06 17:16
274 查看
Per Programming Python, 3rd Edition, there are a number of methods to store persistent data with Python:
I often use flat files to read or write text (string) data using the os
library.
Flat files are read sequentially, but dbm files allow
for keyed access to string data
The pickle module can be used to store non-string Python
data structures, such as Python dicts. However, the data is not keyed as with dbm files.
shelve files combine the best of the dbm and pickle methods
by storing pickled objects in dbm keyed files.
I've read good things about the ZODB object-oriented database,
but I don't know too much about it. Per the book, it is a more powerful alternative to shelves.
The final option is interfacing with a full-fledged SQL relational databases. As I mentioned
before, Python 2.5 has an interface to SQLite as part of the standard distribution.
Here is an example using
Results:
I often use flat files to read or write text (string) data using the os
library.
Flat files are read sequentially, but dbm files allow
for keyed access to string data
The pickle module can be used to store non-string Python
data structures, such as Python dicts. However, the data is not keyed as with dbm files.
shelve files combine the best of the dbm and pickle methods
by storing pickled objects in dbm keyed files.
I've read good things about the ZODB object-oriented database,
but I don't know too much about it. Per the book, it is a more powerful alternative to shelves.
The final option is interfacing with a full-fledged SQL relational databases. As I mentioned
before, Python 2.5 has an interface to SQLite as part of the standard distribution.
Here is an example using
picklewhich writes a Python dict to a file and reads it back again:
import pickle # write python dict to a file mydict = {'a': 1, 'b': 2, 'c': 3} output = open('myfile.pkl', 'wb') pickle.dump(mydict, output) output.close() # read python dict back from the file pkl_file = open('myfile.pkl', 'rb') mydict2 = pickle.load(pkl_file) pkl_file.close() print mydict print mydict2
Results:
{'a': 1, 'c': 3, 'b': 2} {'a': 1, 'c': 3, 'b': 2}
相关文章推荐
- File Uploading to Server Hard Disk using ASP.NET
- Saving and Displaying Photos in SQL Server using ASP.NET and FileUpload Control
- Rapid Web Applications with TurboGears: Using Python to Create Ajax-Powered Sites
- Save an XML File to Database and Send an Email using XSLT
- How to get the file's icon using C++ - 用C++如何获取文件的图标
- HOW TO: Upload a File to a Web Server in ASP.NET by Using Visual Basic .NET
- ExtractionError: Can’t extract file(s) to egg cache, [Errno 13] Permission denied: ‘/root/.python-eggs’
- AJAX file uploads in Rails using attachment_fu and responds_to_parent
- Using mplayer to play mov file
- How to parse XML file using CParser class
- (By Marco Nanni)Using XML to Improve File-Upload Processing
- Openvms-C-Sourcecode- How to Find which Processes are using a specific pagefile or swapfile
- Save Picture from Clipboard to file using C#
- How to Modify the web.config file in SharePoint using SPWebConfigModification?
- Convert stardict dict to txt file
- DICK原创:Rendering an IHTMLElement to a Image File Using GDI+
- prjGetWriteINIFile - How to Read-Write INI file using VB & API - 读取/写入INI文件 - VB6 + API - SourceCode - HackerJLY
- Read and Write compressed data to a binary file using ASP.NET
- FileMaker Web Publishing: A Complete Guide to Using the API for PHP
- Using a batch file to automatically build a Windows CE 5.0 and 6.0 project