2016-06-21 17 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() 

[OK]をので、私はいくつかのコードを書いたが、私は今、大きな問題に直面しています、私はそれを見つけました私のコードではありませんでしたが、plistのLast Backup Dateはエラーを引き起こします。私は文字列だけをparshことができる方法は、あなたが

答えて

0

を迷っている場合、このplistのはplistファイルがまったくうまく形式のXMLではありませんので、私ことのiTunesで作ったところで<date/>

のような他には何もありませんそれがうまくいくか疑問に思う。この場合には次にplの鍵は、あなたが実際にあなたのファイルで定義されたキーになります

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

ので、「アップルカーバージョン」:私は、ルートのplist要素とdictのラッパー(最低でも)あなたが必要だと思います:

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

またplistファイル形式の詳細についてのplist(5)https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man5/plist.5.htmlを参照してください – abeyer

+0

OK答えを更新しました。構文エラーが発生しました:NameError:name 'load'が定義されていません。他のモジュールをインポートする必要はありますか? – UserBOBBED

+0

私はいくつかのコードを書きましたが、現在はより大きな問題に直面しています。私のコードではなく、plistのLast Backup Dateが原因でエラーが発生しました。 UserBOBBED

関連する問題