Writing a dict file in txt and reading it?

I am trying to write a dictionary in a txt file. Then read the dict values ​​by typing the keys using raw_input . I feel like I just skipped one step, but now I was looking for a while.

I get this error

 File "name.py", line 24, in reading print whip[name] TypeError: string indices must be integers, not str 

My code is:

 #!/usr/bin/env python from sys import exit class Person(object): def __init__(self): self.name = "" self.address = "" self.phone = "" self.age = "" self.whip = {} def writing(self): self.whip[p.name] = p.age, p.address, p.phone target = open('deed.txt', 'a') target.write(str(self.whip)) print self.whip def reading(self): self.whip = open('deed.txt', 'r').read() name = raw_input("> ") if name in self.whip: print self.whip[name] p = Person() while True: print "Type:\n\t*read to read data base\n\t*write to write to data base\n\t*exit to exit" action = raw_input("\n> ") if "write" in action: p.name = raw_input("Name?\n> ") p.phone = raw_input("Phone Number?\n> ") p.age = raw_input("Age?\n> ") p.address = raw_input("Address?\n>") p.writing() elif "read" in action: p.reading() elif "exit" in action: exit(0) 
+72
python
Jun 14 2018-12-12T00:
source share
6 answers

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.

+51
Jun 14 '12 at 5:20
source share

Have you tried json module ? The JSON format is very similar to the python dictionary. And it is read / write:

 >>> import json >>> d = {"one":1, "two":2} >>> json.dump(d, open("text.txt",'w')) 

This code is downloaded to a text file

 $ cat text.txt {"two": 2, "one": 1} 

You can also download from a JSON file:

 >>> d2 = json.load(open("text.txt")) >>> print d2 {u'two': 2, u'one': 1} 
+126
Jun 14 '12 at 5:14
source share

To store Python objects in files, use the pickle module:

 import pickle a = { 'a': 1, 'b': 2 } with open('file.txt', 'wb') as handle: pickle.dump(a, handle) with open('file.txt', 'rb') as handle: b = pickle.loads(handle.read()) print a == b # True 

Note that I never set b = a , but instead poisoned a with a file and then scattered it in b .

As for your mistake:

 self.whip = open('deed.txt', 'r').read() 

self.whip was a dictionary object. deed.txt contains text, so when you load the contents of deed.txt into self.whip , self.whip becomes a string representation of itself.

You probably want to evaluate the string back to the Python object:

 self.whip = eval(open('deed.txt', 'r').read()) 

Notice how eval sounds like evil . This is intentional. Use the pickle module instead.

+55
Jun 14 '12 at 5:14
source share

I created my own functions that work very nicely:

 def writeDict(dict, filename, sep): with open(filename, "a") as f: for i in dict.keys(): f.write(i + " " + sep.join([str(x) for x in dict[i]]) + "\n") 

First it will save the key name, and then all the values. Please note that in this case my dict contains integers, so it is converted to int . Most likely, this is the part that you need to change for your situation.

 def readDict(filename, sep): with open(filename, "r") as f: dict = {} for line in f: values = line.split(sep) dict[values[0]] = {int(x) for x in values[1:len(values)]} return(dict) 
+6
Sep 15 '13 at 8:27
source share

You can iterate through a key-value pair and write it to a file

 pair = {'name': name,'location': location} with open('F:\\twitter.json', 'a') as f: f.writelines('{}:{}'.format(k,v) for k, v in pair.items()) f.write('\n') 
+3
Dec 09 '15 at 1:50
source share

Hi, there is a way to write and read the dictionary into a file, you can turn your dictionary into JSON format and quickly and quickly read and write:

to write your date:

  import json your_dictionary = {"some_date" : "date"} f = open('destFile.txt', 'w+') f.write(json.dumps(yout_dictionary)) 

and read your details:

  import json f = open('destFile.txt', 'r') your_dictionary = json.loads(f.read()) 
+1
Aug 12 '17 at 5:37 on
source share



All Articles