0

I have the following variable:

VarX=700

I now need to write the name of VarX into a txt file. So I'm thinking I need to create another variable and assign to it the name of VarX and then write it to a file. How can I assign a name of a variable to a variable?

jpcgandre
  • 1,487
  • 5
  • 31
  • 55

2 Answers2

6

If you store your information as a dictionary:

variables = {'VarX': 700}

then you have access to the name and value without obfuscating your code.

Dolphiniac
  • 1,632
  • 1
  • 9
  • 9
1

So you have a value, and you want to associate it with a name. That's what we call a key/value pair, and in Python, you should use a dict (dictionary) to store that.

http://www.developer.com/lang/other/article.php/630721/Learn-to-Program-Using-Python-Getting-Started-with-Dictionaries.htm

I recommend you use JSON format to save it to disk. You can put your key/value pairs into a dict, then save the whole dict to a JSON file. It's easy and reliable.

https://stackoverflow.com/questions/4759634/python-json-tutorial

Community
  • 1
  • 1
steveha
  • 74,789
  • 21
  • 92
  • 117