Hi All Again I am struck with an Issue if the plist file has multiple dictionary in it then I am not able to get the value of particular key element for example, In the below plist file I wanted to get the value of stepID & COMPONENTNAME key.
I was trying like this for key in plist: if 'results' in key: stepIDValue = key['results']['Steps']['stepID'] I am getting error "string indices must be integers, not str". Please help me out PLIST FILE :::: <?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>commandStatus</key> <string>success</string> <key>comments</key> <string>Get Test Script</string> <key>results</key> <array> <dict> <key>Keywords</key> <string>No keywords.</string> <key>Steps</key> <array> <dict> <key>activeFlag</key> <string>0</string> <key>expectedResult</key> <string></string> <key>expectedTime</key> <string>0000:00:00</string> <key>instructions</key> <string>Launch</string> <key>stepData</key> <string>Test APP</string> <key>stepID</key> <string>7663365</string> <key>stepNumber</key> <string>1</string> <key>stepPriority</key> <string>5</string> <key>summary</key> <string></string> <key>title</key> <string>Launching SDM Application</string> </dict> </array> <key>columns</key> <dict> <key>ACTIVE_FLAG</key> <string>1</string> <key>BUSINESS_CATEGORY</key> <string></string> <key>CHANGE_MANAGEMENT_ID</key> <string></string> <key>COMPONENTNAME</key> <string>Software Client</string> <key>COMPREVVERS</key> <string>Admin</string> <key>CREATION_DATE</key> <string>15-Mar-2011 04:52:01</string> <key>LAST_UPDATE_DATE</key> <string>15-Mar-2011 06:06:52</string> <key>PRIORITY</key> <string>5</string> <key>REGRESSION</key> <string></string> <key>STATUS_ID</key> <string>1</string> <key>TEST_LEVEL</key> <string></string> <key>TEST_REGION</key> <string></string> <key>TEST_SCRIPT_TITLE</key> <string>Software_Auto</string> <key>TRACK_NAME</key> <string></string> </dict> </dict> </array> </dict> </array> </plist> On Tue, Mar 15, 2011 at 11:03 AM, Python User <python.user...@gmail.com>wrote: > Hi Vinay, > > Thanks a Lot it worked for me :) > > Thanks Once Again.... > > > On Tue, Mar 15, 2011 at 1:27 AM, Vinay Shastry <vinayshas...@gmail.com>wrote: > >> On 15 March 2011 01:21, Vinay Shastry <vinayshas...@gmail.com> wrote: >> > On 14 March 2011 17:21, Python User <python.user...@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? >> > >> >> Oh, and your DTD is wrong. >> >> -- >> Vinay S Shastry >> _______________________________________________ >> BangPypers mailing list >> BangPypers@python.org >> http://mail.python.org/mailman/listinfo/bangpypers >> > > _______________________________________________ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers