Generate and parse Mac OS X .plist files using Python (plistlib)

PythonServer Side ProgrammingProgramming

Files with '.plist' the extension is used by Mac OS X applications to store application properties. The plislib module provides an interface to read/write operations of these property list files.

The plist file format serializes basic object types, like dictionaries, lists, numbers, and strings. Usually, the top level object is a dictionary. To write out and to parse a plist file, use the dump() and load() functions. Serialized byte strings are handled by use dumps() and loads() functions. Values can be strings, integers, floats, booleans, tuples, lists, dictionaries (but only with string keys).

This module defines the following functions −

load()Read a plist file pointed by readable and binary file object. format of the file and the following values are valid
  • None − Autodetect the file format

  • FMT_XML − XML file format

  • FMT_BINARY − Binary plist format

dump()Write value to a plist file refereed by writable, binary file object. The fmt argument specifies the format of the plist file and can be one of the following values
  • FMT_XML − XML formatted plist file

  • FMT_BINARY − Binary formatted plist file

loads()Load a plist from a bytes object. See load() for an explanation of the keyword arguments.
dumps()Return value as a plist-formatted bytes object. See the documentation for dump() for an explanation of the keyword arguments of this function.

Following script stores serialized dictionary in plist file

import plistlib
properties = {
   "name" : "Ramesh",
   "College":"ABC College",
   "Class":"FY",
   "marks" : {"phy":60, "che":60, "maths":60}
}
fileName=open('prpos.plist','wb')
plistlib.dump(pl, fileName)
fileName.close()

To read plist file use load() function

with open('marks.plist', 'rb') as fp:
   pl = plistlib.load(fp)
   print(pl)
raja
Published on 19-Mar-2019 10:40:45
Advertisements