用Python读取二进制Plist文件

2024-05-15 07:46:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在使用Plistlib模块来读取Plist文件,但当涉及到二进制Plist文件时,我目前有一个问题。

我想把数据读入一个字符串,以便以后分析/打印等。我想知道他们是否在不使用plutil函数的情况下读入二进制Plist文件并将二进制文件转换成XML?

谢谢你的帮助和提前的时间。


Tags: 模块文件数据函数字符串时间二进制情况
3条回答

Biplisthttps://github.com/wooster/biplist可通过简易安装或pip获得。

尽管您没有指定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)

您可以使用工具plutil(来自http://www.libimobiledevice.org/的libplist)将二进制文件转换为xml plist文件(反之亦然)。

相关问题 更多 >