#-*- coding:gbk -*-
# data = {
# '北京':{
# "昌平":{
# "沙河":["oldboy","test"],
# "天通苑":["链家地产","我爱我家"]
# },
# "朝阳":{
# "望京":["奔驰","陌陌"],
# "国贸":{"CICC","HP"},
# "东直门":{"Advent","飞信"},
# },
# "海淀":{},
# },
# '山东':{
# "德州":{},
# "青岛":{},
# "济南":{}
# },
# '广东':{
# "东莞":{},
# "常熟":{},
# "佛山":{},
# },
# }
def info_change(info):
f1 = open("data.txt","w")
f1.write(info)
f1.close()
f = open("data.txt","r",encoding="gbk")
data_str = f.read()
data = eval(data_str)
flag = 1
while flag:
for i in data:
print(i)
print("i or I 插入数据")
print("c or C 修改数据")
print("d or D 删除数据")
choices = input("choices:")
if choices == "i" or choices == "I":
add_city_one = input("增加一级城市:")
a = {add_city_one:{}}
data.update(a)
data = str(data)
info_change(data)
data = eval(data)
elif choices == "c" or choices == "C":
change_city_one = input("修改一级城市:")
a = {change_city_one: {}}
data.update(a)
data = str(data)
info_change(data)
data = eval(data)
elif choices == "d" or choices == "D":
delete_city_one = input("删除一级城市:")
data.pop(delete_city_one)
data = str(data)
info_change(data)
data = eval(data)
elif choices == "b" or choices== "B"or choices == "q"or choices == "Q":
flag = 0
# elif data[choices] == {}:
# print("无数据")
# flag = False
while choices in data and flag:
for i in data[choices]:
print(i)
print("i or I 插入数据")
print("c or C 修改数据")
print("d or D 删除数据")
choice1 = input("choice1:")
if choice1 == "i" or choice1 == "I":
add_city_two = input("增加二级城市:")
a = {add_city_two: {}}
data[choices].update(a)
data = str(data)
info_change(data)
data = eval(data)
elif choice1 == "c" or choice1 == "C":
change_city_two = input("修改二级城市:")
a = {change_city_two: {}}
data[choices].update(a)
data = str(data)
info_change(data)
data = eval(data)
elif choice1 == "d" or choice1 == "D":
change_city_two = input("删除二级城市:")
data[choices].pop(change_city_two)
data = str(data)
info_change(data)
data = eval(data)
elif choice1 == "b" or choice1 == "B":
break
elif choice1 == "q" or choice1 == "Q":
flag = False
elif data.get(choices).get(choice1) != {}:
pass
# elif data.get(choices).get(choice1) == {}:
# print("无数据")
# flag = False
while choice1 in data[choices] and flag:
for i in data[choices][choice1]:
print(i)
print("i or I 插入数据")
print("c or C 修改数据")
print("d or D 删除数据")
choice2 = input("choice2:")
if choice2 == "i" or choice2 == "I":
add_city_three = input("增加三级城市:")
a = {add_city_three: {}}
data[choices][choice1].update(a)
data = str(data)
info_change(data)
data = eval(data)
elif choice2 == "c" or choice2 == "C":
change_city_three = input("修改三级城市:")
a = {change_city_three: {}}
data[choices][choice1].update(a)
data = str(data)
info_change(data)
data = eval(data)
elif choice2 == "d" or choice2 == "D":
change_city_three = input("删除三级城市:")
data[choices][choice1].pop(change_city_three)
data = str(data)
info_change(data)
data = eval(data)
elif choice2 == "b" or choice2 == "B":
break
elif choice2 == "q" or choice2 == "Q":
flag = 0
elif data.get(choices).get(choice1).get(choice2) != {}:
pass
# elif data.get(choices).get(choice1).get(choice2) == {}:
# print("无数据")
# flag = False
while choice2 in data[choices][choice1] and flag:
for i in data[choices][choice1][choice2]:
print(i)
# choice3 = input("END,请输入b返回")
print("i or I 插入数据")
print("c or C 修改数据")
print("d or D 删除数据")
choice3 = input("choice3:")
if choice3 == "i" or choice3 == "I":
add_city_four = input("增加四级城市:")
a = {add_city_four: {}}
data[choices][choice1][choice2].update(a)
data = str(data)
info_change(data)
data = eval(data)
elif choice3 == "c" or choice3 == "C":
add_city_four = input("修改四级城市:")
a = {add_city_four: {}}
data[choices][choice1][choice2].update(a)
data = str(data)
info_change(data)
data = eval(data)
elif choice3 == "d" or choice3 == "D":
add_city_four = input("删除四级城市:")
data[choices][choice1][choice2].pop(add_city_four)
data = str(data)
info_change(data)
data = eval(data)
if choice3 == "b" or choice3 == "B":
break
elif choice3 == "q" or choice3 == "Q":
flag = 0
请将注释内的数据写入data.txt文件
缺点:重复代码多