pistacchio
pistacchio

Reputation: 58923

Modifying plist in Python

With plistlib I can serialize a dictionary / list structure into a plist. This works ok and I can also read it back with the same library.

The problem is that dictionaries are of type "_internalDict" and I don't seem to be able to change them. For exampl, for example:

d = plistlib.readPlist('someplist.plist')
v = d['value'] # v is an _internalDict
v['val'] = 'new val' # works
del v # doesn't work
v = {'someotherkey': 'someothervalue'} # doesn't work either

The plist doesn't seem to change. Help?

Upvotes: 0

Views: 2836

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1124170

You'll need to delete the key from the d dict:

del d['value']

By setting v = d['value'] you are only creating a new variable that points to the same value as d['value'], but deleting v will not remove the dict from the parent structure.

To replace the dict altogether, you again need to manipulate the parent dict:

d['value'] = { 'foo': 'bar' }

If you execute v = { ... } what you are doing is assigning a reference to a new dict value to the variable v, replacing the reference to the d['value'] dict; you are not manipulating the original value in the d dict.

plistlib._InternalDict is just a subclass of dict that gives warnings for attribute access, which is now deprecated; otherwise it acts just like a regular dict type.

Upvotes: 3

Related Questions