plistlib.rst 7.22 KB

:mod:`plistlib` --- Generate and parse Mac OS X .plist files

Source code: :source:`Lib/plistlib.py`


This module provides an interface for reading and writing the "property list" files used mainly by Mac OS X and supports both binary and XML plist files.

The property list (.plist) file format is a simple serialization supporting 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 :func:`dump` and :func:`load` functions.

To work with plist data in bytes objects, use :func:`dumps` and :func:`loads`.

Values can be strings, integers, floats, booleans, tuples, lists, dictionaries (but only with string keys), :class:`Data`, :class:`bytes`, :class:`bytesarray` or :class:`datetime.datetime` objects.

This module defines the following functions:

The following functions are deprecated:

The following classes are available:

Return an extended mapping object with the same value as dictionary dict.

This class is a subclass of :class:`dict` where attribute access can be used to access items. That is, aDict.key is the same as aDict['key'] for getting, setting and deleting items in the mapping.

Return a "data" wrapper object around the bytes object data. This is used in functions converting from/to plists to represent the <data> type available in plists.

It has one attribute, :attr:`data`, that can be used to retrieve the Python bytes object stored in it.

The following constants are available:

Examples

Generating a plist:

pl = dict(
    aString = "Doodah",
    aList = ["A", "B", 12, 32.1, [1, 2, 3]],
    aFloat = 0.1,
    anInt = 728,
    aDict = dict(
        anotherString = "<hello & hi there!>",
        aThirdString = "M\xe4ssig, Ma\xdf",
        aTrueValue = True,
        aFalseValue = False,
    ),
    someData = b"<binary gunk>",
    someMoreData = b"<lots of binary gunk>" * 10,
    aDate = datetime.datetime.fromtimestamp(time.mktime(time.gmtime())),
)
with open(fileName, 'wb') as fp:
    dump(pl, fp)

Parsing a plist:

with open(fileName, 'rb') as fp:
    pl = load(fp)
print(pl["aKey"])