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
> from plistlib import readPlist
> dict=readPlist('/path/file.plist')
> --> arbitrarily ordered dictionary compared to the XML file
Right. The items in a dict are unordered, and when serialised to a list
they will appear in an arbitrary, unpredictable order.
>>> foo = dict(
... [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)])
>>> foo
{'a': 1, 'c': 3, 'b': 2, 'e': 5, 'd': 4}
> from collections import OrderedDict
> OrderedDict(readPlist('/path/file.plist'))
> --> essentially does the same thing as the previous
Yes, because you are initialising an OrderedDict instance from a dict.
That accesses the dict items as an iterable, which of course is going to
retrieve the items in an arbitrary, unpredictable order.
>>> foo.items()
dict_items([('a', 1), ('c', 3), ('b', 2), ('e', 5), ('d', 4)])
The OrderedDict then faithfully remembers the arbitrary ordering of the
items.
>>> from collections import OrderedDict
>>> OrderedDict(foo.items())
OrderedDict([('a', 1), ('c', 3), ('b', 2), ('e', 5), ('d', 4)])
> readPlist seems to do the scrambling
Nothing “does” the scrambling; the order is forgotten by the dict as
soon as the items go in.
> I "upgraded" to Py3 to have OrderedDict, so please don't say it is
> impossible...
What you'll need to do is insert the items into the OrderedDict instance
in the desired order. You will, of course, need to define what that
desired order is.
>>> sorted(foo.items(), key=(lambda item: item[0]))
[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)]
>>> OrderedDict(sorted(foo.items(), key=(lambda item: item[0])))
OrderedDict([('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)])
--
\ “When we talk to God, we're praying. When God talks to us, |
`\ we're schizophrenic.” —Jane Wagner, via Lily Tomlin, 1985 |
_o__) |
Ben Finney
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.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
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
http://docs.python.org/library/plistlib.html
--
Ned Deily,
n...@acm.org
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
> 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