-2

I have a python file that communicates with a device and access its parameters. As I have to access more than 100 parameters, I have stored those parameter names into one column in csv file. I want to access values assigned to these parameters. But what I am getting the parameters names instead of its values.

My csv file is like

parameter1
parameter2
parameter3
parameter4
...

I am using the following code:

import csv
TagList = []

with open('parameters.csv', 'rb') as file:
    reader = csv.reader(file)
    for row in reader:
        TagList.append(row[0])

for tag in TagList:
    print tag

Output:

parameter1
parameter2
parameter3
parameter4
...

What I want to access is the data assigned to these parameters. I can access its value through its name. i.e.

print parameter1
output: 1.0

I have tried print "%d" %(tag), that is also not working. Please help me in that. Thanks in advance.

svfat
  • 1,873
  • 13
  • 29
Laconic87
  • 15
  • 3
  • where are the values in your csv example? – svfat Aug 20 '15 at 14:30
  • the values are stored in the device, a controller. – Laconic87 Aug 20 '15 at 14:31
  • 1
    You have a csv file with parameter names, and obviously if you run through it in the loop, you can get only parameter names. You should write function which can get parameter value from the device. – svfat Aug 20 '15 at 14:35
  • Thanks for your reply, let me put my question more simpler. # I have two variables variable1 = 0; variable2 = "true" # I am getting a list contaning these variables names as string. # I want to access my defined variables from this list List = ["variable1","variable2"] for item in List: print item output: variable1 variable2 But i want the output as 0 and true. How can i get that? – Laconic87 Aug 20 '15 at 16:01

1 Answers1

0

I'm not sure what the interface for your controller looks like, however I think you would be looking to use reflection here.

so TagList[0] sounds like it is equal to "parameter1" at this point.

If you access your controller through a python class you'd be looking to do something like getattr(controller, TagList[0].

Here is a code example that shows how that would work:

class foo(object):
    x = 3

var = "x"
print getattr(foo(), var)

EDIT:

Okay from what you are saying it sounds like it would be more like this:

variable1 = 0
variable2 = "true"

paramList = ["variable1", "variable2"]
for param in paramList:
    print eval(param)

The docstring for eval() is: Evaluate the source in the context of globals and locals. So this should pick up those variables if they do not belong to an instance.

Also see How to get the value of a variable given its name in a string? which has an answer using globals()['a'] to narrow the scope of the evaluation.

Community
  • 1
  • 1
Ryan
  • 95
  • 9
  • Thanks for your reply, let me put my question more simpler. # I have two variables variable1 = 0; variable2 = "true" I am getting a list contaning these variables names as string. I want to access my defined variables from this list. List = ["variable1","variable2"] for item in List: print item output: variable1 variable2 But i want the output as 0 and true. How can i get that? – Laconic87 Aug 20 '15 at 16:41
  • I added another example where the variables aren't members of an object. Let me know if that works – Ryan Aug 20 '15 at 17:23
  • Hi Ryan, it works with eval(param). Thanks a lot for your help. – Laconic87 Aug 20 '15 at 18:03
  • No problem! Please accept this answer using the green checkbox so that anybody else looking for similar info will see that it worked for you! – Ryan Aug 20 '15 at 18:17