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?
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:

- 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") do I get the above print statement to print the contents:
...instead of the literal string:
"AWfromJSONfile" ?

Thanks much in advance!
If fromJSONfile is a variable, You can do any of these:
print(project + str(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)
Or in one line:
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]
chatguy likes this post
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],}
chatguy likes this post
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!!
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!!
Mandatory reading: Keep data out of your variable names and also
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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Replacing String Variable with a new String Name kevv11 2 894 Jul-29-2023, 12:03 PM
Last Post: snippsat
  convert string to float in list jacklee26 6 2,108 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,328 Jan-10-2023, 11:26 AM
Last Post: codinglearner
  python r string for variable mg24 3 3,217 Oct-28-2022, 04:19 AM
Last Post: deanhystad
  how to convert tuple value into string mg24 2 2,581 Oct-06-2022, 08:13 AM
Last Post: DeaD_EyE
  USE string data as a variable NAME rokorps 1 1,051 Sep-30-2022, 01:08 PM
Last Post: deanhystad
  Removing Space between variable and string in Python coder_sw99 6 6,557 Aug-23-2022, 01:15 PM
Last Post: louries
  Remove a space between a string and variable in print sie 5 1,958 Jul-27-2022, 02:36 PM
Last Post: deanhystad
  Split string using variable found in a list japo85 2 1,422 Jul-11-2022, 08:52 AM
Last Post: japo85
  Convert string to float problem vasik006 8 3,657 Jun-03-2022, 06:41 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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