plistlib
— 生成和剖析 Mac OS X
.plist
文件
¶
源代码: 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.
特性列表 (
.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
dump()
and
load()
函数。
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
,
bytesarray
or
datetime.datetime
对象。
3.4 版改变: New API, old API deprecated. Support for binary format plists added.
3.8 版改变:
Support added for reading and writing
UID
tokens in binary plists as used by NSKeyedArchiver and NSKeyedUnarchiver.
另请参阅
Apple 的文件格式文档编制。
此模块定义了下列函数:
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 a dictionary).
The fmt is the format of the file and the following values are valid:
None
: Autodetect the file format
FMT_XML
:XML 文件格式
FMT_BINARY
:二进制 plist 格式
若
use_builtin_types
is true (the default) binary data will be returned as instances of
bytes
, otherwise it is returned as instances of
Data
.
The dict_type is the type used for dictionaries that are read from the plist file.
XML data for the
FMT_XML
format is parsed using the Expat parser from
xml.parsers.expat
– see its documentation for possible exceptions on ill-formed XML. Unknown elements will simply be ignored by the plist parser.
The parser for the binary format raises
InvalidFileException
when 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 of the keyword arguments.
3.4 版新增。
plistlib.
dump
(
value
,
fp
,
*
,
fmt=FMT_XML
,
sort_keys=True
,
skipkeys=False
)
¶
写入 value to a plist file. Fp should be a 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
当 sort_keys is true (the default) the keys for dictionaries will be written to the plist in sorted order, otherwise they will be written in the iteration order of the dictionary.
当
skipkeys
is false (the default) the function raises
TypeError
when 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 or a container that contains objects of unsupported types.
An
OverflowError
will be raised for integer values that cannot be represented in (binary) plist files.
3.4 版新增。
plistlib.
dumps
(
value
,
*
,
fmt=FMT_XML
,
sort_keys=True
,
skipkeys=False
)
¶
返回
value
as a plist-formatted bytes object. See the documentation for
dump()
for an explanation of the keyword arguments 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 (readable and binary) file object. Returns the unpacked root object (which usually is a dictionary).
此函数调用
load()
to do the actual work, see the documentation of
that function
for an explanation of the keyword arguments.
从 3.4 版起弃用:
使用
load()
代替。
3.7 版改变: Dict values in the result are now normal dicts. You no longer can use attribute access to access items of these dictionaries.
plistlib.
writePlist
(
rootObject
,
pathOrFile
)
¶
写入 rootObject to an XML plist file. pathOrFile may be either a file name or a (writable and binary) file object
从 3.4 版起弃用:
使用
dump()
代替。
plistlib.
readPlistFromBytes
(
data
)
¶
Read a plist data from a bytes object. Return the root object.
见
load()
for a description of the keyword arguments.
从 3.4 版起弃用:
使用
loads()
代替。
3.7 版改变: Dict values in the result are now normal dicts. You no longer can use attribute access to access items of these dictionaries.
plistlib.
writePlistToBytes
(
rootObject
)
¶
返回 rootObject as an XML plist-formatted bytes object.
从 3.4 版起弃用:
使用
dumps()
代替。
The following classes are available:
plistlib.
数据
(
data
)
¶
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,
data
, that can be used to retrieve the Python bytes object stored in it.
从 3.4 版起弃用:
Use a
bytes
object instead.
plistlib.
UID
(
data
)
¶
Wraps an
int
. This is used when reading or writing NSKeyedArchiver encoded data, which contains UID (see PList manual).
It has one attribute,
data
, which can be used to retrieve the int value of the UID.
data
must be in the range
0 <= data < 2**64
.
3.8 版新增。
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 版新增。
生成 .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"])