- Python Data Persistence - Home
- Python Data Persistence - Introduction
- Python Data Persistence - File API
- File Handling with os Module
- Python Data Persistence - Object Serialization
- Python Data Persistence - Pickle Module
- Python Data Persistence - Marshal Module
- Python Data Persistence - Shelve Module
- Python Data Persistence - dbm Package
- Python Data Persistence - CSV Module
- Python Data Persistence - JSON Module
- Python Data Persistence - XML Parsers
- Python Data Persistence - Plistlib Module
- Python Data Persistence - Sqlite3 Module
- Python Data Persistence - SQLAlchemy
- Python Data Persistence - PyMongo module
- Python Data Persistence - Cassandra Driver
- Data Persistence - ZODB
- Data Persistence - Openpyxl Module
Python Data Persistence Resources
Python Data Persistence - Plistlib Module
The plist format is mainly used by MAC OS X. These files are basically XML documents. They store and retrieve properties of an object. Python library contains plist module, that is used to read and write 'property list' files (they usually have .plist' extension).
The plistlib module is more or less similar to other serialization libraries in the sense, it also provides dumps() and loads() functions for string representation of Python objects and load() and dump() functions for disk operation.
Following dictionary object maintains property (key) and corresponding value −
proplist = {
"name" : "Ganesh",
"designation":"manager",
"dept":"accts",
"salary" : {"basic":12000, "da":4000, "hra":800}
}
Example - Writing to plist file
In order to write these properties in a disk file, we call dump() function in plist module.
main.py
import plistlib
fileName=open('salary.plist','wb')
proplist = {
"name" : "Ganesh",
"designation":"manager",
"dept":"accts",
"salary" : {"basic":12000, "da":4000, "hra":800}
}
plistlib.dump(proplist, fileName)
fileName.close()
Example - Reading a plist file
Conversely, to read back the property values, use load() function as follows −
main.py
import plistlib
fp= open('salary.plist', 'rb')
pl = plistlib.load(fp)
print(pl)
Output
{'dept': 'accts', 'designation': 'manager', 'name': 'Ganesh', 'salary': {'basic': 12000, 'da': 4000, 'hra': 800}}
Advertisements