Your code is almost right! You are right, you just skipped one step. When you read in a file, you read it as a string; but you want to turn the string back into a dictionary.
The error message you saw was because self.whip was a string, not a dictionary.
At first I wrote that you can just substitute the string in dict() , but that will not work! You need to do something else.
Example
Here is the easiest way: feed the string to eval() . For example:
def reading(self): s = open('deed.txt', 'r').read() self.whip = eval(s)
You can do this in one line, but I think it looks dirty:
def reading(self): self.whip = eval(open('deed.txt', 'r').read())
But eval() sometimes not recommended. The problem is that eval() will evaluate any string, and if someone tricks you into running a really complex string, something bad could happen. In this case, you just run eval() in your own file, so everything should be fine.
But since eval() is useful, someone made an alternative to this, which is safer. This is called literal_eval , and you get it from a Python module called ast .
import ast def reading(self): s = open('deed.txt', 'r').read() self.whip = ast.literal_eval(s)
ast.literal_eval() will only evaluate strings that turn into basic Python types, so there is no way for a complex string to do anything bad on your computer.
EDIT
Actually, the best practice in Python is to use the with statement to make sure the file is closed correctly. Rewriting the above to use the with statement:
import ast def reading(self): with open('deed.txt', 'r') as f: s = f.read() self.whip = ast.literal_eval(s)
In the most popular Python known as "CPython", you usually don't need a with statement, since the built-in garbage collection function will determine that you are done with the file and close it for you. But other Python implementations, such as "Jython" (Python for Java VM) or "PyPy" (a really cool experimental system with code optimization only at a point in time), may not find a way to close the file for you. It's good to get used to using with , and I think this makes the code very easy to understand.