plistlib
.plist
就业培训 下载中心 Wiki 联络 登录 注册 首页 Python 3.12.4 索引 模块 下一 上一 Python 标准库 文件格式 plistlib — 生成和剖析 Apple .plist 文件 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 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), bytes , bytearray 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. 3.9 版改变: 移除旧 API。 另请参阅 PList 手册页 Apple 的文件格式文档编制。 此模块定义了下列函数: plistlib. load ( fp , * , fmt = None , 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 格式 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. Added in version 3.4. plistlib. loads ( data , * , fmt = None , dict_type = dict ) ¶ Load a plist from a bytes object. See load() for an explanation of the keyword arguments. Added in version 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. Added in version 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. Added in version 3.4. The following classes are available: class 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 . Added in version 3.8. The following constants are available: plistlib. FMT_XML ¶ The XML format for plist files. Added in version 3.4. plistlib. FMT_BINARY ¶ The binary format for plist files Added in version 3.4. 范例 ¶ 生成 .plist: import datetime import plistlib 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.now() ) print(plistlib.dumps(pl).decode()) Parsing a plist: import plistlib plist = b"""<plist version="1.0"> <dict> <key>foo</key> <string>bar</string> </dict> </plist>""" pl = plistlib.loads(plist) print(pl["foo"]) 内容表 plistlib — 生成和剖析 Apple .plist 文件 范例 上一话题 netrc — netrc 文件处理 下一话题 加密服务 本页 报告 Bug 展示源 快速搜索 键入搜索术语或模块、类、函数名称。 首页 Python 3.12.4 索引 模块 下一 上一 Python 标准库 文件格式 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 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() 函数。
dump()
load()
To work with plist data in bytes objects, use dumps() and loads() .
dumps()
loads()
Values can be strings, integers, floats, booleans, tuples, lists, dictionaries (but only with string keys), bytes , bytearray or datetime.datetime 对象。
bytes
bytearray
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.
UID
3.9 版改变: 移除旧 API。
另请参阅
Apple 的文件格式文档编制。
此模块定义了下列函数:
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
None
FMT_XML :XML 文件格式
FMT_XML
FMT_BINARY :二进制 plist 格式
FMT_BINARY
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.
xml.parsers.expat
The parser for the binary format raises InvalidFileException when the file cannot be parsed.
InvalidFileException
Added in version 3.4.
Load a plist from a bytes object. See load() for an explanation of the keyword arguments.
写入 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.
TypeError
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.
OverflowError
返回 value as a plist-formatted bytes object. See the documentation for dump() for an explanation of the keyword arguments of this function.
The following classes are available:
Wraps an int . This is used when reading or writing NSKeyedArchiver encoded data, which contains UID (see PList manual).
int
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 .
data
0 <= data < 2**64
Added in version 3.8.
The following constants are available:
The XML format for plist files.
The binary format for plist files
生成 .plist:
import datetime import plistlib 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.now() ) print(plistlib.dumps(pl).decode())
Parsing a plist:
import plistlib plist = b"""<plist version="1.0"> <dict> <key>foo</key> <string>bar</string> </dict> </plist>""" pl = plistlib.loads(plist) print(pl["foo"])
netrc — netrc 文件处理
netrc
加密服务
键入搜索术语或模块、类、函数名称。