- Python Data Persistence - Discussion
- Python Data Persistence - Useful Resources
- Python Data Persistence - Quick Guide
- Data Persistence - Openpyxl Module
- Data Persistence - ZODB
- Python Data Persistence - Cassandra Driver
- Python Data Persistence - PyMongo module
- Python Data Persistence - SQLAlchemy
- Python Data Persistence - Sqlite3 Module
- Python Data Persistence - Plistlib Module
- Python Data Persistence - XML Parsers
- Python Data Persistence - JSON Module
- Python Data Persistence - CSV Module
- Python Data Persistence - dbm Package
- Python Data Persistence - Shelve Module
- Python Data Persistence - Marshal Module
- Python Data Persistence - Pickle Module
- Python Data Persistence - Object Serialization
- File Handling with os Module
- Python Data Persistence - File API
- Python Data Persistence - Introduction
- Python Data Persistence - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Python Data Persistence - Object Seriapzation
Python s built-in file object returned by Python s built-in open() function has one important shortcoming. When opened with w mode, the write() method accepts only the string object.
That means, if you have data represented in any non-string form, the object of either in built-in classes (numbers, dictionary, psts or tuples) or other user-defined classes, it cannot be written to file directly. Before writing, you need to convert it in its string representation.
numbers=[10,20,30,40] file=open( numbers.txt , w ) file.write(str(numbers)) file.close()
For a binary file, argument to write() method must be a byte object. For example, the pst of integers is converted to bytes by bytearray() function and then written to file.
numbers=[10,20,30,40] data=bytearray(numbers) file.write(data) file.close()
To read back data from the file in the respective data type, reverse conversion needs to be done.
file=open( numbers.txt , rb ) data=file.read() print (pst(data))
This type of manual conversion, of an object to string or byte format (and vice versa) is very cumbersome and tedious. It is possible to store the state of a Python object in the form of byte stream directly to a file, or memory stream and retrieve to its original state. This process is called seriapzation and de-seriapzation.
Python’s built in pbrary contains various modules for seriapzation and deseriapzation process.
Sr.No. | Name & Description |
---|---|
1 |
pickle Python specific seriapzation pbrary |
2 |
marshal Library used internally for seriapzation |
3 |
shelve Pythonic object persistence |
4 |
dbm pbrary offering interface to Unix database |
5 |
csv pbrary for storage and retrieval of Python data to CSV format |
6 |
json Library for seriapzation to universal JSON format |