[Tutor] Importing XML files.

mhysnm1964 at gmail.com mhysnm1964 at gmail.com
Mon Jan 21 18:39:00 EST 2019


Peter and Alan,

Peter, Thanks for the information. The library did the trick and I can get access to the XML content.

Alan, thanks for the explanation of the tree structure. I was aware of this already for HTML and XML. Just didn't understand the terminology used from the XML library. The tutorials I have viewed didn't explain this information. I will continue investigating. The library provided has addressed my concerns for now.


-----Original Message-----
From: Tutor <tutor-bounces+mhysnm1964=gmail.com at python.org> On Behalf Of Peter Otten
Sent: Tuesday, 22 January 2019 5:22 AM
To: tutor at python.org
Subject: Re: [Tutor] Importing XML files.

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.


_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list