[BangPypers] plist files

Vinay Shastry vinayshastry at gmail.com
Mon Mar 14 20:51:05 CET 2011


On 14 March 2011 17:21, Python User <python.user.01 at gmail.com> wrote:
> Hi All,
>
> I have a plist file, test.plist, All I want is read the scriptNO tag value
> 12345 and replace it with 67899. Can any one please help me out for
> this. The file content is
>
> <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE plist PUBLIC "-//test//EN" "
> http://www.test.com/DTDs/PropertyList-1.0.dtd">
> <plist version="1.0">
> <array>
> <dict>
> <key>command</key>
> <string>GetTestScriptData</string>
> <key>comments</key>
> <string>Get Test Script</string>
> <key>opParam</key>
> <dict>
> <key>scriptNO</key>
> <string>12345</string>
> <key>columnNames</key>
> <string>Test case</string>
> <key>fetchKey</key>
> <string>1</string>
> <key>fetchData</key>
> <string>1</string>
> </dict>
> </dict>
> </array>
> </plist>
>
> I was using the below code to read the plist file, but I am not able  to
> search the scriptNo and replace it with other value.
>
> import plistlib
> file = ('/Users/me/Desktop/test.plist')
> plist = plistlib.readPlist(file)
> print plist
>
> Please help me out to solve this. Thanks In Advance.

The answer is in your plist format itself. Everything is enclosed in
an array - which becomes a list in py.

How about something like this:

for key in plist:
    if 'opParam' in key and 'scriptNO' in key['opParam']:
        key['opParam']['scriptNO'] = 67899

plistlib.writePlist(plist, "mynewfile.plist")


Also, is there a reason why file is a tuple containing filename, and
not just a string?

-- 
Vinay S Shastry


More information about the BangPypers mailing list