[Tutor] Importing XML files.
Peter Otten
__peter__ at web.de
Mon Jan 21 13:22:09 EST 2019
mhysnm1964 at gmail.com wrote:
> I am trying to import ITunes XML files. I have been doing some reading and
> I am somewhat confused with some XML terminology.
> What is:
>
>
>
> Tag – I think it is the <key> in the itunes library file.
>
> Text = The text between the <key>
>
> Attrib -- don’t know.
>
>
>
> If I am correct in my thinking. If you look up all the tags using the xml
> module. How do you then isolate all the name text? I do not have any
> working code at this present time. I have been playing with the xml
> methods and reading some example tutorials. But I am stumped with the
> above terminology which is a small road block. Below is an example extract
> of my xML ITunes to see if this helps. I am doing this in Python 3.7 for
> Windows 10.
>
>
>
> <?xml version="1.0" encoding="UTF-8"?>
>
> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
> "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
>
> <plist version="1.0">
You may be lucky in that you can avoid the hard way outlined by Alan --
Python's stdandard library includes a module that handles Apple's plist
format. I tried your sample, and the module seems to turn it into nested
dicts:
>>> import plistlib, pprint
>>> with open("sample.xml", "rb") as f:
... data = plistlib.load(f)
...
>>> pprint.pprint(data, width=70)
{'Application Version': '12.8.0.150',
'Date': datetime.datetime(2019, 1, 14, 3, 56, 30),
'Features': 5,
'Library Persistent ID': 'F2D33B339F0788F0',
'Major Version': 1,
'Minor Version': 1,
'Music Folder': 'file:///Volumes/Itunes/iTunes/iTunes%20Media/',
'Show Content Ratings': True,
'Tracks': {'6493': {'Album': 'In Her Sights (Unabridged)',
'Album Artist': 'Robin Perini',
'Artist': 'Robin Perini',
'Artwork Count': 1,
'Bit Rate': 64,
'Comments': "Jasmine 'Jazz' Parker, "
'Jefferson County SWAT’s only '
'female sniper, can thread the '
'eye of a needle with a bullet. '
'But she carries with her a '
'secret from her past....',
[snip]
>>> data["Date"].isoformat()
'2019-01-14T03:56:30'
>>> list(data["Tracks"].values())[0]["Artist"]
'Robin Perini'
Looks good, except for the mojibake -- but that was already in your email.
More information about the Tutor
mailing list