plistlib --- 產生和剖析 Apple.plist 檔案¶
原始碼:Lib/plistlib.py
This module provides an interface for reading and writing the "property list"files used by Apple, primarily on macOS and iOS. This module supports both binaryand 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 thedump() andload() functions.
To work with plist data in bytes or string objects, usedumps()andloads().
Values can be strings, integers, floats, booleans, tuples, lists, dictionaries(but only with string keys),bytes,bytearrayordatetime.datetime objects.
在 3.4 版的變更:New API, old API deprecated. Support for binary format plists added.
在 3.8 版的變更:Support added for reading and writingUID tokens in binary plists as usedby NSKeyedArchiver and NSKeyedUnarchiver.
在 3.9 版的變更:Old API removed.
也參考
- PList manual page
Apple's documentation of the file format.
此模組定義了以下函式:
- plistlib.load(fp,*,fmt=None,dict_type=dict,aware_datetime=False)¶
Read a plist file.fp should be a readable and binary file object.Return the unpacked root object (which usually is adictionary).
Thefmt is the format of the file and the following values are valid:
None:自動偵測檔案格式FMT_XML:XML 檔案格式FMT_BINARY:二進位 plist 格式
Thedict_type is the type used for dictionaries that are read from theplist file.
Whenaware_datetime is true, fields with type
datetime.datetimewillbe created asaware object, withtzinfoasdatetime.UTC.XML data for the
FMT_XMLformat is parsed using the Expat parserfromxml.parsers.expat-- see its documentation for possibleexceptions on ill-formed XML. Unknown elements will simply be ignoredby the plist parser.The parser raises
InvalidFileExceptionwhen the file cannot be parsed.在 3.4 版被加入.
在 3.13 版的變更:新增僅限關鍵字參數aware_datetime。
- plistlib.loads(data,*,fmt=None,dict_type=dict,aware_datetime=False)¶
Load a plist from a bytes or string object. See
load()for anexplanation of the keyword arguments.在 3.4 版被加入.
在 3.13 版的變更:data 在fmt 等於
FMT_XML時可以是一個字串。
- plistlib.dump(value,fp,*,fmt=FMT_XML,sort_keys=True,skipkeys=False,aware_datetime=False)¶
Writevalue to a plist file.fp should be a writable, binaryfile object.
Thefmt argument specifies the format of the plist file and can beone of the following values:
FMT_XML: XML formatted plist fileFMT_BINARY: Binary formatted plist file
Whensort_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.
Whenskipkeys is false (the default) the function raises
TypeErrorwhen a key of a dictionary is not a string, otherwise such keys are skipped.Whenaware_datetime is true and any field with type
datetime.datetimeis set as anaware object, it will convert toUTC timezone before writing it.A
TypeErrorwill be raised if the object is of an unsupported type ora container that contains objects of unsupported types.An
OverflowErrorwill be raised for integer values that cannotbe represented in (binary) plist files.在 3.4 版被加入.
在 3.13 版的變更:新增僅限關鍵字參數aware_datetime。
- plistlib.dumps(value,*,fmt=FMT_XML,sort_keys=True,skipkeys=False,aware_datetime=False)¶
Returnvalue as a plist-formatted bytes object. Seethe documentation for
dump()for an explanation of the keywordarguments of this function.在 3.4 版被加入.
The following classes are available:
- classplistlib.UID(data)¶
Wraps an
int. This is used when reading or writing NSKeyedArchiverencoded data, which contains UID (see PList manual).- data¶
Int value of the UID. It must be in the range
0<=data<2**64.
在 3.8 版被加入.
以下常數可供使用:
- plistlib.FMT_XML¶
The XML format for plist files.
在 3.4 版被加入.
- plistlib.FMT_BINARY¶
The binary format for plist files
在 3.4 版被加入.
這個模組定義了以下例外:
- exceptionplistlib.InvalidFileException¶
當檔案無法被剖析時引發。
在 3.4 版被加入.
範例¶
Generating a plist:
importdatetimeimportplistlibpl=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.now())print(plistlib.dumps(pl).decode())
Parsing a plist:
importplistlibplist=b"""<plist version="1.0"><dict> <key>foo</key> <string>bar</string></dict></plist>"""pl=plistlib.loads(plist)print(pl["foo"])