[Pythonmac-SIG] parsing system_profiler xml output

brad.allen at omsdal.com brad.allen at omsdal.com
Fri Nov 12 01:45:13 CET 2004

eichin at metacarta.com wrote:
> > (as for the other part, Python's XML story is wierd - when PyXML is
> > installed it drops some new things into the xml namespace, so having
> > "import xml" work doesn't actually tell you anything useful.)

Ah, thanks for the clarification.

Bob Ippolito <bob at redivi.com> wrote:
> import _xmlplus is the correct way to detect the presence of PyXML.

I tried this, which didn't work on my system, so I guess PyXML is not part
of the Panther Python distribution. I don't really want to fuss with
distributing this to all our Macs if I can avoid it.

Bob Ippolito <bob at redivi.com> wrote:
> If this doesn't work with Python 2.4's plistlib, then the output of
> system profiler is not correct.  I'm at least 90% sure that Python
> 2.4's plistlib correctly reads and writes all valid plists.

Well, you may be right. I don't have an XML validator handy at the moment,
but I tried your updated, bug-fixed plistlib on the full output of
system_profiler -xml. It failed, but I didn't get the same traceback as
eichin. Also, plistlib worked beautifully on partial output of
system_profiler, such as system_profiler -SPNetworkDataType -xml.

Here's what I did:

At the bash prompt, ran: system_profiler -xml >

In Python:
      import pyDesktopConfig.plistlibTest as plistlib
      plistDict =

....after which I received the following error:

oms-ballen:/OMS/IT_Tools/scripts/managedScript/pythonTools ballen$ python
Traceback (most recent call last):
  File "tester2.py", line 60, in ?
    plistDict = plistlib.readPlist('/Users/ballen/spoutput.txt')
 line 79, in readPlist
    rootObject = p.parse(pathOrFile)
 line 383, in parse
xml.parsers.expat.ExpatError: not well-formed (invalid token): line 13031,
column 15

Next, I tried:

In Bash: system_profiler SPNetworkDataType -xml > spNetwork.plist
In Python:
      plistDict = plistlib.readPlist('/Users/ballen/spNetwork.plist')
      print plistDict

[{'_timeStamp': Date('2004-11-12T35:16:00Z'), '_detailLevel': -1, '_items':
[{'domain': 'omsdal.com', 'dns_servers': ['', ''],
'router_address': '', 'broadcast_address': [''],
'type': 'Ethernet', 'ethernet_address': '00:0a:93:9d:76:f8', '_name':
'Built-in Ethernet', 'subnet_mask': [''], 'interface': 'en0',
'ip_address': ['']}, {'interface': 'modem', 'type': 'PPP
(PPPSerial)', '_name': 'Internal Modem'}, {'interface': 'en1', 'type':
'AirPort', 'ethernet_address': '00:31:62:0b:47:34', '_name': 'AirPort'}],
'_dataType': 'SPNetworkDataType', '_properties': {'domain': {'_order':
'80'}, 'dns_servers': {'_order': '70'}, 'router_address': {'_order': '60'},
'broadcast_address': {'_order': '50'}, 'type': {'_order': '20',
'_isColumn': 'YES', '_width': '100'}, 'ethernet_address': {'_order': '90'},
'_name': {'_order': '0', '_isColumn': 'YES', '_width': '100'},
'subnet_mask': {'_order': '40'}, 'interface': {'_order': '10', '_isColumn':
'YES', '_width': '100'}, 'destination_address': {'_order': '35'},
'ip_address': {'_order': '30', '_isColumn': 'YES', '_width': '100'}}}]

Very nice!

More information about the Pythonmac-SIG mailing list