保存python plistlib数据(修订版)
如何使用此工作代码:保存python plistlib数据(修订版),python,Python,如何使用此工作代码: import plistlib, time import zipfile from contextlib import closing import os with closing(zipfile.ZipFile("fw.zip")) as zfile: for info in zfile.infolist(): if info.filename.endswith('Restore.plist'): zfile.e
import plistlib, time
import zipfile
from contextlib import closing
import os
with closing(zipfile.ZipFile("fw.zip")) as zfile:
for info in zfile.infolist():
if info.filename.endswith('Restore.plist'):
zfile.extract(info)
import plistlib as pl
p=pl.readPlist("Restore.plist")
print p["ProductType"]
print p["ProductVersion"]
print p["ProductBuildVersion"]
outputfile = open('output.txt', 'w')
outputfile.write( p["ProductVersion"] )
outputfile.write( ' ')
outputfile.write( p["ProductType"] )
outputfile.write( ' ')
outputfile.write( p["ProductBuildVersion"] )
outputfile.close()
然后用它写出这个plist中“update”键下的字符串,看看update键在另一组名为“RestoreRamDisks”的键下是如何工作的
RestoreRamDisks#这是“更新”键所在的键
更新#这是我说的更新密钥
018-7074-092.dmg#这就是我想让python吐出来的东西
使用者
018-7082-092.dmg
为了澄清,我只想使用上面相同的方法,这次,获取“更新”键的信息。让我困惑的是,“update”键位于另一个名为“RestoreRamDisks”的键之下。我想让这个程序在被要求定位“更新”键时吐出018-7074-092.dmg。当您打印p[“RestoreRamDisks”]时会发生什么?你可以通过简单的操作来获得价值
p["RestoreRamDisks"]["Update"]
# If your plist may not contain a key called "RestoreRamDisks"
# use this instead
p.get("RestoreRamDisks", dict(Update=None)).get("Update", None)
# This will get the value if available, otherwise it will
# evaluate to None
p["RestoreRamDisks"]["Update"]
# If your plist may not contain a key called "RestoreRamDisks"
# use this instead
p.get("RestoreRamDisks", dict(Update=None)).get("Update", None)
# This will get the value if available, otherwise it will
# evaluate to None