Python update variable name in for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Python update variable name in for loop
# 1  
Old 07-01-2015
Tools Python update variable name in for loop

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.
# 2  
Old 07-02-2015
Check your python documentation for the 'exec' function. if you can't use child[index] and have to use hard names, exec is your friend for creating on the fly code (normally you would not do this though.. but there are some cases...).
# 3  
Old 07-03-2015
Instead of using sequentially named variables, why not use a list (if you don't need to start with 19), or a dict?
Code:
child = {}
child[19] = 'value 19'
child[20] = 'value 20'

for x in range(19,20):
    child[x] = 'another value ' + str(x)

Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assign Values to a Variable in While Loop and Update the File

Hello, Could anyone please help me with Assigning a value to variable and then updating the value in the original file IFS='|' while read -r Serial_ID JOB_NAME STATUS do if then echo "Perform Fuctions" ???Assign STATUS to COMPLETED and Update File??? done <File (7 Replies)
Discussion started by: infernalhell
7 Replies

2. Linux

Unable to access CentOS setup Network Configuration after attempting Python update

I attempted to update Python on my CentOS 5.6 VM and have encountered some issues after then. Initially yum wouldn't work, but I have resolved that. However, now I am unable to access the Network Configuration section under setup. When I do select this option I see this at the command line: ... (2 Replies)
Discussion started by: crmpicco
2 Replies

3. Red Hat

Python 2.6 on redhat 5 (update 8)

Hi, I would like to know wheather it is possible to install python 2.6 on RedHat Ent Linux 5 (update8) using rpms? I have checked by default Python 2.4 is installed. or wheather Redhat 6.0 needs to be installed for python 2.6? (1 Reply)
Discussion started by: manoj.solaris
1 Replies

4. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

5. UNIX for Dummies Questions & Answers

Python update already printed line.

Hi. I have a basic script in python that outputs like this.. $ ./test.py 1% 2% 3% 4% 5% 6% 7% 8% 9% 10% ... But how can I make it so the output stays in 1 line? So it would look something like this.. $ ./test.py 10% (1 Reply)
Discussion started by: cbreiny
1 Replies

6. Shell Programming and Scripting

loop in array in python

Hi suppose in python I have a list(or array, or tuple, not sure the difference) How do I loop inside the size of array. The pseudo code is: a= for i = 1 to dim(a) print a end How to find the dimension in python? Also, anyone has a handbook to suggest so I can borrow from library (1 Reply)
Discussion started by: grossgermany
1 Replies

7. Shell Programming and Scripting

how to pass a variable to an update sql statement inside a loop

hi all, i am experiencing an error which i think an incorrect syntax for the where clause passing a variable was given. under is my code. sqlplus -s ${USERNAME}/${PASSWORD}@${SID} << END1 >> $LOGFILE whenever sqlerror exit set serveroutput on size 1000000 declare l_rc ... (0 Replies)
Discussion started by: ryukishin_17
0 Replies

8. Shell Programming and Scripting

ksh: A part of variable A's name is inside of variable B, how to update A?

This is what I tried: vara=${varb}_count (( vara += 1 )) Thanks for help (4 Replies)
Discussion started by: pa3be
4 Replies
Login or Register to Ask a Question