title: Best way to retrieve variable values from a text file?
body: <p>Referring on <a href="https://stackoverflow.com/questions/868112/loading-files-into-variables-in-python">this question</a>, I have a similar -but not the same- problem..</p>
  
  <p>On my way, I'll have some text file, structured like:</p>
  
  <pre><code>var_a: 'home'
  var_b: 'car'
  var_c: 15.5
  </code></pre>
  
  <p>And I need that python read the file and then create a variable named var_a with value 'home', and so on.</p>
  
  <p>Example:</p>
  
  <pre><code>#python stuff over here
  getVarFromFile(filename) #this is the function that im looking for
  print var_b
  #output: car, as string
  print var_c
  #output 15.5, as number.
  </code></pre>
  
  <p>Is this possible, I mean, even keep the var type?</p>
  
  <p>Notice that I have the full freedom to the text file structure, I can use the format I like if the one I proposed isn't the best.</p>
  
  <p><strong>EDIT</strong>: the ConfigParser can be a solution, but I don't like it so much, because in my script I'll have then to refer to the variables in the file with</p>
  
  <pre><code>config.get("set", "var_name")
  </code></pre>
  
  <p>But what I'll love is to refer to the variable directly, as I declared it in the python script...</p>
  
  <p>There is a way to import the file as a python dictionary?</p>
  
  <p>Oh, last thing, keep in mind that I don't know exactly how many variables would I have in the text file.</p>
  
  <p><strong>Edit 2</strong>: I'm very interested at stephan's JSON solution, because in that way the text file could be read simply with others languages (PHP, then via AJAX JavaScript, for example), but I fail in something while acting that solution:</p>
  
  <pre><code>#for the example, i dont load the file but create a var with the supposed file content
  file_content = "'var_a': 4, 'var_b': 'a string'"
  mydict = dict(file_content)
  #Error: ValueError: dictionary update sequence element #0 has length 1; 2 is required
  file_content_2 = "{'var_a': 4, 'var_b': 'a string'}"
  mydict_2 = dict(json.dump(file_content_2, True))
  #Error:
  #Traceback (most recent call last):
  #File "&lt;pyshell#5&gt;", line 1, in &lt;module&gt;
  #mydict_2 = dict(json.dump(file_content_2, True))
  #File "C:\Python26\lib\json\__init__.py", line 181, in dump
  #fp.write(chunk)
  #AttributeError: 'bool' object has no attribute 'write'
  </code></pre>
  
  <p>In what kind of issues can I fall with the JSON format?
  And, how can I read a JSON array in a text file, and transform it in a python dict?</p>
  
  <p>P.S: I don't like the solution using .py files; I'll prefer .txt, .inc, .whatever is not restrictive to one language.</p>