解析bplist文件

本文档介绍了在解析bplist文件时遇到的问题,通过使用github上的plist2json项目,作者发现程序在尝试转换二进制plist到JSON时遇到了`dictionary keys changed during iteration`的错误。经过分析,问题出在代码尝试修改迭代中的字典。为解决这个问题,作者建议将迭代对象从字典改为列表,避免在迭代过程中修改字典。通过这个修改,程序得以正常执行。
摘要由CSDN通过智能技术生成

        平时用 mac 的基本都接触过 plist 文件,它一般用作项目的配置文件,而 bplist 文件就是它的二进制形式。

        如果需要解析 bplist 文件内容,可以参考 github 上的 plist2json项目,但如果直接执行程序,会提示 mapkey 不允许改变:

lucas@lucasdeMacBook-Pro plist2json % ll
total 112
-rw-r--r--  1 lucas  staff   1994 11 15 18:39 Info.plist
-rw-r--r--  1 lucas  staff   1062 11 15 18:39 LICENSE
-rw-r--r--  1 lucas  staff   6001 11 15 18:39 test.bplist
drwxr-xr-x  7 lucas  staff    224 11 29 21:19 plist
-rw-r--r--  1 lucas  staff    767 11 15 18:39 readme.md
-rw-r--r--  1 lucas  staff    723 11 15 18:58 test.py
lucas@lucasdeMacBook-Pro plist2json % python3 test.py
Traceback (most recent call last):
  File "test.py", line 28, in <module>
    testBPlist()
  File "test.py", line 20, in testBPlist
    print(convertPlistToJson(content));
  File "test.py", line 10, in convertPlistToJson
    bplist = BPlist(plistContent)
  File "/Users/lucas/www/plist2json/plist/bplist.py", line 30, in __init__
    self.parseData()
  File "/Users/lucas/www/plist2json/plist/bplist.py", line 60, in parseData
    for key, val in obj.items():
RuntimeError: dictionary keys changed during iteration
lucas@lucasdeMacBook-Pro plist2json %

        根据提示,第 60 行附近程序如下:

if type(obj) == dict:
    for key, val in obj.items():
        if type(key) == Index:
            obj.pop(key)
            obj[self.objectList[key.index]] = self.objectList[val.index]

        此处只需将 map 改为 list ,即:

for key, val in list(obj.items()):
    if type(key) == Index:
        obj.pop(key)

        再重新执行程序就 ok 了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值