使用Python读取二进制Plist文件

4 投票
5 回答
10115 浏览
提问于 2025-04-17 10:12

我现在正在使用Plistlib模块来读取Plist文件,但在处理二进制Plist文件时遇到了一些问题。

我想把数据读取成一个字符串,以便后续分析或打印等。我在想有没有办法直接读取二进制Plist文件,而不使用plutil函数把二进制文件转换成XML格式?

谢谢你们的帮助和时间!

5 个回答

3

Biplist 是一个可以通过 easy-install 或 pip 安装的工具,链接在这里:https://github.com/wooster/biplist

4

我可能晚了10年才来回答这个问题,不过对于任何正在寻找这个的人来说,plistlib就是你需要的东西。

7

虽然你没有提到 plutil,但提供一个使用它的有效解决方案可能对其他人有帮助,因为它在Mac上是预装的:

import json
from subprocess import Popen, PIPE

def plist_to_dictionary(filename):
    "Pipe the binary plist through plutil and parse the JSON output"
    with open(filename, "rb") as f:
        content = f.read()
    args = ["plutil", "-convert", "json", "-o", "-", "--", "-"]
    p = Popen(args, stdin=PIPE, stdout=PIPE)
    out, err = p.communicate(content)
    return json.loads(out)

print plist_to_dictionary(path_to_plist_file)

撰写回答