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?

3
  • 2
    data = {'VarX': 700} Commented May 3, 2013 at 18:50
  • Search for "python list tutorial" or "python dict tutorial", perhaps. There are some fundamental concepts that should be explored independently. Commented May 3, 2013 at 18:51
  • This is what you need stackoverflow.com/questions/1534504/… Commented May 3, 2013 at 18:54

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.