2011-09-13 42 views
1

我有一個zip文件。在該zip中,我有一個plist文件,「Restore.plist」。我怎麼能告訴Python閱讀 說的.plist文件並停止當它達到這一部分:閱讀.plist文件數據並用python記錄到內存中

<key>SupportedProductTypes</key> 
<array> 
    <string>iPhone3,1</string> 
</array> 

我想告訴Python停在「SupportedProductTypes」鍵,記錄其相應的字符串,「iPhone3,1 「以內存作爲變量,例如」x「 然後打印x。我怎樣才能做到這一點?

回答

3

plistlib在Python標準庫中處理.plist文件。有關快速教程,請參閱this PyMOTW article

另請參閱zipfile模塊(也在Python標準庫中)的ZipFile類,用於從ZIP文件讀取數據。

1

嘗試用XML:

import xml.etree.ElementTree as ET 

tree = ET.parse("keys.xml") 
doc = tree.getroot() 
sup = doc.getiterator("SupportedProductTypes") 

print doc.getiterator("string")[0].text 

給出:

iPhone3,1 

與這個名爲 「keys.xml」 測試文件:

<keys> 
<key>SupportedProductTypes</key> 
<array> 
    <string>iPhone3,1</string> 
</array> 
</keys> 
+0

它說AttributeError的: 'NoneType' 對象有沒有屬性'attrib' – user715578

+0

沒錯。我很抱歉,我再次匆忙....查看編輯解決方案!請享用。 – Louis

+0

嗯..現在我得到「keys.xml沒有這樣的文件或目錄。」對不起... – user715578