UserBOBBED
UserBOBBED

Reputation: 39

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

Upvotes: 2

Views: 5773

Answers (1)

abeyer
abeyer

Reputation: 745

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'])

Upvotes: 2

Related Questions