Python Forum
How to convert string to variable?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to convert string to variable?
#1
Question 
Hi all,

I've had a hard time searching for this answer (Google suggests something different than I'm looking for).

VERY early in my code, I have dicts read in from a JSON file, resulting in:
  AWfromJSONfile=[1,20,300,4,5]
  AAfromJSONfile=[50,60,7,8,9]
  MAfromJSONfile=[300,400,50,60,70]

- MUCH later, I have a string variable called:
project <-- This could be either "AW", "AA", or "MA"

- For example:
project="AW" #<----(for example)

print(project + "fromJSONfile")
...how do I get the above print statement to print the contents:
1,20,300,4,5
...instead of the literal string:
"AWfromJSONfile" ?

Thanks much in advance!
CG
Reply
#2
If fromJSONfile is a variable, You can do any of these:
print(project + str(fromJSONfile))
print("f{project}{fromJSONfile})
print(project, fromJSONfile, sep="")
If you are trying to put AW + fromJSONfile to make a variable name, and then get the value for that variable, you can do this:
varname = project+"fromJSONfile"
value = locals().get(varname)
print(value)
Or in one line:
print(locals.get(project+"fromJSONfile"))
locals() creates a dictionary of variables in the module. It would be better if you did this yourself when you read the json data so you had something like this:
jsondata ={    # Modify json so it creates a nice dictionary with keys
    "AW": [1,20,300,4,5],
    "AA": [50,60,7,8,9],
    "MA": [300,400,50,60,70]
}
print(jsondata[project]
chatguy likes this post
Reply
#3
You could do
print(globals()[project + "fromJSONfile"])
but this is not very good python, using globals() or locals() is usually frowned upon.

Instead you could define your own dictionary
fromJSONfile = {
    'AW': [1,20,300,4,5],
    'AA': [50,60,7,8,9],
    'MA': [300,400,50,60,70],}
Then
print(fromJSONfile[project])
chatguy likes this post
Reply
#4
Thumbs Up 
Thank you, Dean!!

You are awesome!! I was banging my head on this for hours, and you figured out in 5 seconds! Thank you!!

In case anyone else is searching for this answer, I found I needed to make a small modification to the last example above, to get it to execute without error:
value = locals().get(project+"fromJSONfile")
Thanks again!!
CG
Reply
#5
Wow, Gribouillis.... that is an awesome idea!!

It's going to involve some work elsewhere in the code, but I am definitely going to follow this in my next update. Thank you!!
Reply
#6
Mandatory reading: Keep data out of your variable names and also http://stupidpythonideas.blogspot.com/20...reate.html
well, you are not creating the names in a loop, but it is basically the same problem.

Just read the JSON and use it (maybe after some transformation to dict as suggested by @Gribouillis).

btw, it's interesting to see sample of the JSON structure
ndc85430 likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Replacing String Variable with a new String Name kevv11 2 792 Jul-29-2023, 12:03 PM
Last Post: snippsat
  convert string to float in list jacklee26 6 1,922 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  Need help on how to include single quotes on data of variable string hani_hms 5 2,046 Jan-10-2023, 11:26 AM
Last Post: codinglearner
  python r string for variable mg24 3 2,835 Oct-28-2022, 04:19 AM
Last Post: deanhystad
  how to convert tuple value into string mg24 2 2,357 Oct-06-2022, 08:13 AM
Last Post: DeaD_EyE
  USE string data as a variable NAME rokorps 1 967 Sep-30-2022, 01:08 PM
Last Post: deanhystad
  Removing Space between variable and string in Python coder_sw99 6 6,302 Aug-23-2022, 01:15 PM
Last Post: louries
  Remove a space between a string and variable in print sie 5 1,799 Jul-27-2022, 02:36 PM
Last Post: deanhystad
  Split string using variable found in a list japo85 2 1,306 Jul-11-2022, 08:52 AM
Last Post: japo85
  Convert string to float problem vasik006 8 3,425 Jun-03-2022, 06:41 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020