plistlib —- Generate and parse Mac OS X .plist files

Source code: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 supportingbasic object types, like dictionaries, lists, numbers and strings. Usually thetop level object is a dictionary.

To write out and to parse a plist file, use the dump() andload() functions.

To work with plist data in bytes objects, use dumps()and loads().

Values can be strings, integers, floats, booleans, tuples, lists, dictionaries(but only with string keys), Data, bytes, bytesarrayor datetime.datetime objects.

在 3.4 版更改: New API, old API deprecated. Support for binary format plists added.

参见

这个模块定义了以下函数:

  • plistlib.load(fp, *, fmt=None, use_builtin_types=True, dict_type=dict)
  • Read a plist file. fp should be a readable and binary file object.Return the unpacked root object (which usually is adictionary).

The fmt is the format of the file and the following values are valid:

If use_builtin_types is true (the default) binary data will be returnedas instances of bytes, otherwise it is returned as instances ofData.

The dict_type is the type used for dictionaries that are read from theplist file.

XML data for the FMT_XML format is parsed using the Expat parserfrom xml.parsers.expat — see its documentation for possibleexceptions on ill-formed XML. Unknown elements will simply be ignoredby the plist parser.

The parser for the binary format raises InvalidFileExceptionwhen the file cannot be parsed.

3.4 新版功能.

  • plistlib.loads(data, *, fmt=None, use_builtin_types=True, dict_type=dict)
  • Load a plist from a bytes object. See load() for an explanation ofthe keyword arguments.

3.4 新版功能.

  • plistlib.dump(value, fp, *, fmt=FMT_XML, sort_keys=True, skipkeys=False)
  • Write value to a plist file. Fp should be a writable, binaryfile object.

The fmt argument specifies the format of the plist file and can beone of the following values:

When sort_keys is true (the default) the keys for dictionaries will bewritten to the plist in sorted order, otherwise they will be written inthe iteration order of the dictionary.

When skipkeys is false (the default) the function raises TypeErrorwhen a key of a dictionary is not a string, otherwise such keys are skipped.

A TypeError will be raised if the object is of an unsupported type ora container that contains objects of unsupported types.

An OverflowError will be raised for integer values that cannotbe represented in (binary) plist files.

3.4 新版功能.

  • plistlib.dumps(value, *, fmt=FMT_XML, sort_keys=True, skipkeys=False)
  • Return value as a plist-formatted bytes object. Seethe documentation for dump() for an explanation of the keywordarguments of this function.

3.4 新版功能.

The following functions are deprecated:

  • plistlib.readPlist(pathOrFile)
  • Read a plist file. pathOrFile may be either a file name or a (readableand binary) file object. Returns the unpacked root object (which usuallyis a dictionary).

This function calls load() to do the actual work, see the documentationof that function for an explanation of the keyword arguments.

3.4 版后已移除: Use load() instead.

在 3.7 版更改: Dict values in the result are now normal dicts. You no longer can useattribute access to access items of these dictionaries.

  • plistlib.writePlist(rootObject, pathOrFile)
  • Write rootObject to an XML plist file. pathOrFile may be either a file nameor a (writable and binary) file object

3.4 版后已移除: Use dump() instead.

  • plistlib.readPlistFromBytes(data)
  • Read a plist data from a bytes object. Return the root object.

See load() for a description of the keyword arguments.

3.4 版后已移除: Use loads() instead.

在 3.7 版更改: Dict values in the result are now normal dicts. You no longer can useattribute access to access items of these dictionaries.

  • plistlib.writePlistToBytes(rootObject)
  • Return rootObject as an XML plist-formatted bytes object.

3.4 版后已移除: Use dumps() instead.

The following classes are available:

  • class plistlib.Data(data)
  • Return a "data" wrapper object around the bytes object data. This is usedin functions converting from/to plists to represent the <data> typeavailable in plists.

It has one attribute, data, that can be used to retrieve the Pythonbytes object stored in it.

3.4 版后已移除: Use a bytes object instead.

The following constants are available:

  • plistlib.FMT_XML
  • The XML format for plist files.

3.4 新版功能.

  • plistlib.FMT_BINARY
  • The binary format for plist files

3.4 新版功能.

例子

Generating a plist:

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

Parsing a plist:

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