Quantcast
Channel: UNIX and Linux Forums
Viewing all articles
Browse latest Browse all 16232

Python update variable name in for loop

$
0
0
Hello all,
Not sure if this question has been answered already.

I have some xml Element variable as below:
Code:

child19 = core_elem_dcache.find('stat[@name="read_accesses"]')
    child20 = core_elem_dcache.find('stat[@name="write_accesses"]')
    child21 = core_elem_dcache.find('stat[@name="read_misses"]')
    child22 = core_elem_dcache.find('stat[@name="write_misses"]'

Next I want to change the values of these corresponding elements like below:
Code:

child20.set('value', str(params[19]))
child21.set('value', str(params[20]))
child22.set('value', str(params[21])) ######params is a list already read before from a text file

Now instead of setting setting the values manually for each element, I wanted to do a for loop for a range (10,100). But I am not sure how to update the "child" element dynamically.

I tried following:
Code:

for x in range(13,23):
        child{x}.set('value',str(params[x-1]))

which surely gave me error:
Code:

File "abc.py", line 99
    child{x}.set('value',str(params[x-1]))
        ^
SyntaxError: invalid syntax

Kindly help.

Viewing all articles
Browse latest Browse all 16232

Trending Articles