I am trying to write a dictionary to a txt file. Then read the dict values by typing the keys with raw_input
. I feel like I am just missing one step but I have been looking for a while now.
I get this error
File "name.py", line 24, in reading
print whip[name]
TypeError: string indices must be integers, not str
My code:
#!/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:
*read to read data base
*write to write to data base
*exit to exit"
action = raw_input("
> ")
if "write" in action:
p.name = raw_input("Name?
> ")
p.phone = raw_input("Phone Number?
> ")
p.age = raw_input("Age?
> ")
p.address = raw_input("Address?
>")
p.writing()
elif "read" in action:
p.reading()
elif "exit" in action:
exit(0)
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…