2016-06-21 907 views
0

我的Python代碼有問題。我有一個plist文件。在Python中解析plist文件

的Info.plist

<plist> 
    <dict> 
    <key>Apple Car Version</key> 
    <string>1.0</string> 
    <key>Last Backup Date</key> 
    <date/> 
    <key>Product Type</key> 
    <string>iPad2,5</string> 
    <key>Product Version</key> 
    <string>9.3.1</string> 
    </dict> 
</plist> 

我的Python代碼

import os 
import plistlib 

def main(): 

    fileName=os.path.expanduser('Info.plist') 

    if os.path.exists(fileName): 

     pl=plistlib.readPlist(fileName) 

     if 'Product Version' in pl: 
     print('The aString value is %s\n' % pl['Apple Car Version']) 

     else: 
     print('There is no Apple Car Version in the plist\n') 

    else: 
     print('%s does not exist, so can\'t be read' % fileName) 

if __name__ == '__main__': 
    main() 

好了,所以我寫了一些代碼,但我現在面臨着更大的問題,我發現它不是我的代碼,但我的plist plist中的Last Backup Date會導致錯誤。有沒有辦法,我只能parsh琴絃,沒有別的喜歡<date/>

通過這個plist中是由iTunes進行的方式,如果你想知道

回答

0

這plist文件沒有良好的XML可言,所以我懷疑它會起作用。我認爲你需要(至少)根的plist元素和字典包裝:在這種情況下

<plist> 
    <dict> 
    <key>Apple Car Version</key> 
    <string>1.0</string> 
    </dict> 
</plist> 

隨後的pl鍵將你實際上在你的文件中定義的按鍵,讓「蘋果車版」 :

print(pl['Apple Car Version']) 
+0

另見的plist(5)https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man5/plist.5.html對plist文件格式的詳細信息 – abeyer

+0

好吧,我更新了我的答案,我得到的語法錯誤:NameError:name'load'未定義,我是否需要導入其他模塊? – UserBOBBED

+0

好吧,我寫了一些代碼,但我現在面臨着比較大的問題,我發現它不是我的代碼,但我的plist最後備份日期在plist導致錯誤。有沒有一種方法,我只能解析字符串沒有別的像 UserBOBBED