Rya*_*yan 5 python boolean plist attributeerror
我正在尝试找到一种访问 plist 文件的方法:/Library/Preferences/com.apple.iPod.plist 以访问其中的序列号。
这是我当前的代码--
import os
import plistlib
fileName=os.path.expanduser('/Users/Ryan/Library/Preferences/com.apple.iPod.plist')
pl=plistlib.readPlist(fileName)
for left, right in pl.items():
for values in right.values():
print(values['Serial Number'])
Run Code Online (Sandbox Code Playgroud)
我不断得到结果,但也会出现一些快速错误。我得到这个:
plist.py:8: DeprecationWarning: The readPlist function is deprecated, use load() instead pl=plistlib.readPlist(fileName)
Run Code Online (Sandbox Code Playgroud)
还有这个:
File "plist.py", line 16, in <module>
for values in right.values():
AttributeError: 'bool' object has no attribute 'values'
Run Code Online (Sandbox Code Playgroud)
我猜使用加载函数相当简单,尽管我很难使用我在网上找到的教程来弄清楚它并根据我的需要进行修改。
关于布尔属性错误,我不知道我做错了什么。
谢谢!
要消除弃用错误,请将包含的行替换readPlist为
with open(fileName, 'rb') as f:
pl = plistlib.load(f)
Run Code Online (Sandbox Code Playgroud)
您的第二个问题似乎是由于plistlib的更改所致:
版本 3.7 中的更改:结果中的字典值现在是普通字典。您不再可以使用属性访问来访问这些字典的项目。
我遇到了类似的问题:通过将出现的 替换为 来AttributeError: 'dict' object has no attribute 'children'解决。我想您对 的调用可能会出现类似的情况,但如果没有您期望的 plist 的实际示例,则很难判断。someObj.children[:]someObj['children']right.values()