Serialization
What you are talking about doing is object serialization, and there are better ways of doing it than rolling your own serialization method (though you seem to have come up with YAML). Both of these are still less secure than the ast.literal_eval() approach (pickle particularly), but they definitely should be noted here.
JSON
Here is an example of doing what you want using JSON, a popular cross-language format:
import json
myDict = {'a':1, 'b':2}
# write to the file 'data'
with open('data','w') as f:
json.dump(myDict, f)
# now we can load it back
with open('data','r') as f:
myDictLoaded = json.load(f)
print myDictLoaded
Output:
{u'a': 1, u'b': 2}
pickle
Here is a second example doing the same thing using pickle. pickle is more powerful in that it can serialize all* python objects, even ones you write.
import cPickle as pickle
myDict = {'a':1, 'b':2}
# write to the file 'data'
with open('data','w') as f:
pickle.dump(myDict, f)
# now we can load it back
with open('data','r') as f:
myDictLoaded = pickle.load(f)
print myDictLoaded
Output:
{'a': 1, 'b': 2}