Python 3: Plist as OrderedDict

G

Gnarlodious

I am trying to read a *.plist into Python 3's OrderedDict but can't
figure it out. Saying something like this:

from plistlib import readPlist
dict=readPlist('/path/file.plist')
--> arbitrarily ordered dictionary compared to the XML file

from collections import OrderedDict
OrderedDict(readPlist('/path/file.plist'))
--> essentially does the same thing as the previous
readPlist seems to do the scrambling, is this a non-implementation in
Python 3.1?

I "upgraded" to Py3 to have OrderedDict, so please don't say it is
impossible...

-- Gnarlie
 
B

Benjamin Kaplan

I am trying to read a *.plist into Python 3's OrderedDict but can't
figure it out. Saying something like this:

from plistlib import readPlist
dict=readPlist('/path/file.plist')
--> arbitrarily ordered dictionary compared to the XML file

from collections import OrderedDict
OrderedDict(readPlist('/path/file.plist'))
--> essentially does the same thing as the previous
readPlist seems to do the scrambling, is this a non-implementation in
Python 3.1?

I "upgraded" to Py3 to have OrderedDict, so please don't say it is
impossible...

-- Gnarlie

readPlist returns a dict. That dict is unordered. Wrapping the call in
OrderedDict() doesn't suddenly make readPlist use an ordered dict
instead, it just takes the (unordered) dict and sticks it in a new
OrderedDict. I suppose you could dig into the plistlib source code and
change that to use the OrderedDict if you really need it.
 
T

Terry Reedy

I am trying to read a *.plist into Python 3's OrderedDict but can't
figure it out. Saying something like this:

from plistlib import readPlist

As a general note, include a link or something when discussing a
relatively obscure module that seems not to even be listed in pypi.
dict=readPlist('/path/file.plist')

Redefining a basic builtin name like 'dict' is usually a bad idea.
--> arbitrarily ordered dictionary compared to the XML file

from collections import OrderedDict
OrderedDict(readPlist('/path/file.plist'))
--> essentially does the same thing as the previous
readPlist seems to do the scrambling, is this a non-implementation in
Python 3.1?

I "upgraded" to Py3 to have OrderedDict, so please don't say it is
impossible...

I agree with Benjamin -- check the source. Is plistlib 3.x ready?

Terry Jan Reedy
 
R

Raymond Hettinger

I am trying to read a *.plist into Python 3's OrderedDict but can't
figure it out. Saying something like this: ....
I "upgraded" to Py3 to have OrderedDict, so please don't say it is
impossible...

You may be able to monkey patch an OrderedDict into the PlistParser.
Here's an untested stab at it:

from collections import OrderedDict
import plistlib
plistlib._InteralDict = OrderedDict

Raymond
 
G

Gnarlodious

You may be able to monkey patch an OrderedDict into the PlistParser.
Here's an untested stab at it:

    from collections import OrderedDict
    import plistlib
    plistlib._InteralDict = OrderedDict

Genius! After fixing the misspelled InteralDict I got this:

from collections import OrderedDict
import plistlib
plistlib._InternalDict = OrderedDict
plistlib.readPlist('/path/to/some.plist')

--> OrderedDict([('List', 'of'), ('tuples', 'in'), ('plist',
'order')])

So thank you for that [somewhat contorted] solution. (PyN00b): Now how
do I extract the list from the function?

-- Gnarlie
 
G

Gnarlodious

You may be able to monkey patch an OrderedDict into the PlistParser.
Here's an untested stab at it:
    from collections import OrderedDict
    import plistlib
    plistlib._InteralDict = OrderedDict


Genius! After fixing the misspelled InteralDict I got this:

from collections import OrderedDict
import plistlib
plistlib._InternalDict = OrderedDict
plistlib.readPlist('/path/to/some.plist')
--> OrderedDict([('List', 'of'), ('tuples', 'in'), ('plist',
'order')])

So thank you for that [somewhat contorted] solution.
To extract the list I am saying this:

ordered=plistlib.readPlist(path)
print(list(ordered)) # only a list of keys
print(ordered[list(ordered)[0]])

However this seems too laborious. is there an easier way?

-- Gnarlie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top