Matthew Oujiri
Matthew Oujiri

Reputation: 145

How to parse plists in Python?

I'm trying to parse through a massive file for an iTunes library and I'm attempting to use plistlib library.

My code looks something like this:

import plistlib

with open('iTunes Music Library.xml') as fp:
    pl = plistlib.load(fp)

print(pl["aKey"])

As I do so, I get the error:

TypeError: startswith first arg must be str or a tuple of str, not bytes

I'm not sure what this is, any explanations?

Upvotes: 1

Views: 1448

Answers (2)

isopach
isopach

Reputation: 1938

You need to open the file as a binary, i.e.

with open('iTunes Music Library.plist', 'rb') as fp:
        pl = plistlib.load(fp)

Upvotes: 0

user2357112
user2357112

Reputation: 281683

As stated in the docs, plistlib.load takes a binary file object. You've given it a file opened in text mode.

Upvotes: 2

Related Questions