简体   繁体   中英

Parse plist file in Python

I have an issue with my Python code. I have a plist file.

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>

My Python Code :

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 so I wrote out some code, but I'm facing BIGGER problems now, I found out it wasn't my code but my plist The Last Backup Date in the plist cause errors. Is there a way where I can only parsh the strings, nothing else like <date/>

By the way this plist was made by iTunes if your wondering

That plist file isn't well formed xml at all, so I doubt it will work. I think you need (at a minimum) a root plist element and a dict wrapper:

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

Then the keys of pl will be the keys you actually define in your file, so "Apple Car Version" in this case:

print(pl['Apple Car Version'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM