title: Reading dictionary structure from text file in python
body: <p>I have file where I have defined python dictionary:</p>
  
  <pre><code>dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First', 'Friends': {'Emil':1, 'Frank':0} };
  </code></pre>
  
  <p>I want to read this dictionary and use it's element in python code. I've end up with following code:</p>
  
  <pre><code>#!/usr/bin/python
  
  import sys
  import os
  import ast
  import re
  from pprint import pprint as pp
  
  def readObjFromFile(file):
    with open (file, "r") as myfile:
      data=myfile.read()
    data = re.sub("^#.*", "", data)
    data = data.replace('\n', '')
    data = data.split("=", 1)[1].split(";", 1)[0].lstrip()
    data = ast.literal_eval(data)
    return data
  
  if __name__ == "__main__":
    if len(sys.argv[1:]) == 1:
      dict = readObjFromFile(sys.argv[1])
      print type(dict)
      pp(dict)
    else:
      print "Pass file from which object will be read"
      exit
  </code></pre>
  
  <p>This works also for <a href="http://termbin.com/s4sq" rel="nofollow">this</a> larger dictionary. I want to ask if there is better way to do it? I am aware of <code>pickle</code> module but this is not what I want because it stores data in it's own format. I was considering modules which are used for parsing json due to similarity with python dicts, but I do not know if this approach is safe.</p>