python3 读取.plist文件_plistlib — Generate and parse Mac OS X .plist files

### 导航

- [索引](../genindex.xhtml "总目录")

- [模块](../py-modindex.xhtml "Python 模块索引") |

- [下一页](crypto.xhtml "加密服务") |

- [上一页](xdrlib.xhtml "xdrlib --- Encode and decode XDR data") |

- ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png)

- [Python](https://www.python.org/) »

- zh\_CN 3.7.3 [文档](../index.xhtml) »

- [Python 标准库](index.xhtml) »

- [文件格式](fileformats.xhtml) »

- $('.inline-search').show(0); |

# [`plistlib`](#module-plistlib "plistlib: Generate and parse Mac OS X plist files.") --- Generate and parse Mac OS X `.plist` files

**Source code:** [Lib/plistlib.py](https://github.com/python/cpython/tree/3.7/Lib/plistlib.py) \[https://github.com/python/cpython/tree/3.7/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 [`dump()`](#plistlib.dump "plistlib.dump") and [`load()`](#plistlib.load "plistlib.load") functions.

To work with plist data in bytes objects, use [`dumps()`](#plistlib.dumps "plistlib.dumps")and [`loads()`](#plistlib.loads "plistlib.loads").

Values can be strings, integers, floats, booleans, tuples, lists, dictionaries (but only with string keys), [`Data`](#plistlib.Data "plistlib.Data"), [`bytes`](stdtypes.xhtml#bytes "bytes"), `bytesarray`or [`datetime.datetime`](datetime.xhtml#datetime.datetime "datetime.datetime") objects.

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

参见

[PList manual page](https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/PropertyLists/) \[https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/PropertyLists/\]Apple's documentation of the file format.

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

`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`](constants.xhtml#None "None"): Autodetect the file format

- [`FMT_XML`](#plistlib.FMT_XML "plistlib.FMT_XML"): XML file format

- [`FMT_BINARY`](#plistlib.FMT_BINARY "plistlib.FMT_BINARY"): Binary plist format

If *use\_builtin\_types* is true (the default) binary data will be returned as instances of [`bytes`](stdtypes.xhtml#bytes "bytes"), otherwise it is returned as instances of [`Data`](#plistlib.Data "plistlib.Data").

The *dict\_type* is the type used for dictionaries that are read from the plist file.

XML data for the [`FMT_XML`](#plistlib.FMT_XML "plistlib.FMT_XML") format is parsed using the Expat parser from [`xml.parsers.expat`](pyexpat.xhtml#module-xml.parsers.expat "xml.parsers.expat: An interface to the Expat non-validating XML parser.") -- 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()`](#plistlib.load "plistlib.load") for an explanation of the 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, binary file object.

The *fmt* argument specifies the format of the plist file and can be one of the following values:

- [`FMT_XML`](#plistlib.FMT_XML "plistlib.FMT_XML"): XML formatted plist file

- [`FMT_BINARY`](#plistlib.FMT_BINARY "plistlib.FMT_BINARY"): Binary formatted plist file

When *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.

When *skipkeys* is false (the default) the function raises [`TypeError`](exceptions.xhtml#TypeError "TypeError")when a key of a dictionary is not a string, otherwise such keys are skipped.

A [`TypeError`](exceptions.xhtml#TypeError "TypeError") will be raised if the object is of an unsupported type or a container that contains objects of unsupported types.

An [`OverflowError`](exceptions.xhtml#OverflowError "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*)Return *value* as a plist-formatted bytes object. See the documentation for [`dump()`](#plistlib.dump "plistlib.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).

This function calls [`load()`](#plistlib.load "plistlib.load") to do the actual work, see the documentation of [`that function`](#plistlib.load "plistlib.load") for an explanation of the keyword arguments.

3\.4 版后已移除: Use [`load()`](#plistlib.load "plistlib.load") instead.

在 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*)Write *rootObject* to an XML plist file. *pathOrFile* may be either a file name or a (writable and binary) file object

3\.4 版后已移除: Use [`dump()`](#plistlib.dump "plistlib.dump") instead.

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

See [`load()`](#plistlib.load "plistlib.load") for a description of the keyword arguments.

3\.4 版后已移除: Use [`loads()`](#plistlib.loads "plistlib.loads") instead.

在 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*)Return *rootObject* as an XML plist-formatted bytes object.

3\.4 版后已移除: Use [`dumps()`](#plistlib.dumps "plistlib.dumps") instead.

The following classes are available:

*class* `plistlib.``Data`(*data*)Return a "data" wrapper object around the bytes object *data*. This is used in functions converting from/to plists to represent the `` 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`](stdtypes.xhtml#bytes "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:

```

pl = dict(

aString = "Doodah",

aList = ["A", "B", 12, 32.1, [1, 2, 3]],

aFloat = 0.1,

anInt = 728,

aDict = dict(

anotherString = "",

aThirdString = "M\xe4ssig, Ma\xdf",

aTrueValue = True,

aFalseValue = False,

),

someData = b"",

someMoreData = b"" * 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"])

```

### 导航

- [索引](../genindex.xhtml "总目录")

- [模块](../py-modindex.xhtml "Python 模块索引") |

- [下一页](crypto.xhtml "加密服务") |

- [上一页](xdrlib.xhtml "xdrlib --- Encode and decode XDR data") |

- ![](https://box.kancloud.cn/a721fc7ec672275e257bbbfde49a4d4e_16x16.png)

- [Python](https://www.python.org/) »

- zh\_CN 3.7.3 [文档](../index.xhtml) »

- [Python 标准库](index.xhtml) »

- [文件格式](fileformats.xhtml) »

- $('.inline-search').show(0); |

© [版权所有](../copyright.xhtml) 2001-2019, Python Software Foundation.

Python 软件基金会是一个非盈利组织。 [请捐助。](https://www.python.org/psf/donations/)

最后更新于 5月 21, 2019. [发现了问题](../bugs.xhtml)?

使用[Sphinx](http://sphinx.pocoo.org/)1.8.4 创建。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值