python全栈自学04——通过文件实现三级菜单增删改查

 要求:文本文件中保存省市县三级菜单信息,通过文件实现对三级菜单的增删改查,并保存修改

这是菜单文本

{
    "陕西省":{
        "西安市":["未央区","灞桥区","长安区","雁塔区"],
        "渭南市":["华县","潼关","大荔","白水"],
        "宝鸡市":["岐山","凤翔","扶风","眉县"]
    },
     "四川省":{
         "成都市":["锦江区","青羊区","金牛区","武侯区"],
         "自贡市":["自流井区","贡井区","大安区","沿滩区"],
         "泸州市":["江阳区","纳溪区","泸县","合江"]
     },
     "河南省":{
         "洛阳市":["老城区","西工区","吉利区","郊区"],
         "开封市":["龙亭区","鼓楼区","南关区","兰考县"],
         "郑州市":["中原区","二七区","金水区","上街区"]
     },
     "青海省":{
         "西宁市":["城东区","城西区","城北区","城中区"],
         "海东":["平安","乐都","湟中","湟源"],
         "玉树":["玉树","杂多","称多","治多"]
     },
     "辽宁省":{
         "沈阳市":["和平区","沈河区","大东区","皇姑区"],
         "大连市":["中山区","西岗区","瓦房店","旅顺口"],
         "铁岭市":["银州区","清河区","西丰","开原"]
     }
}

代码如下:

f_read = open('3menu','r',encoding='utf-8')      #打开文件
f_write = open('3menu1','w',encoding='utf-8')     #创建菜单副本(不修改原文件)

dic = f_read.read()    #读取文件,得到字符串格式
a = eval(dic)     #将读到的文件对象f中的内容转换为字典格式(原本就是字典格式,从文件中读是字符串)

back_flag = False   #返回上一级标志位
quit_flag = False   #退出程序标志位

while not quit_flag or back_flag:
    #进入第一级
    for i in a:
        print(i)
    choice = input("1.增 2.删 3.改 4.退出菜单 or 输入省份进入下一级:")
    if choice == "4":
        quit_flag = True
        break
    elif choice in a:
        #进入第二级
        while not quit_flag or back_flag:
            for j in a[choice]:
                print(j)
            choice2 = input("1.增 2.删 3.改 4.返回上级 5.退出菜单 or 输入城市进入下级:")
            if choice2 == "4":
                back_flag = True
                break
            elif choice2 == "5":
                quit_flag = True
                break
            elif choice2 in a[choice]:
                #进入第三级
                while not quit_flag or back_flag:
                    for k in a[choice][choice2]:
                        print(k)
                    choice3 = input("已到达最后一级 1.增 2.删 3.改 4.返回上级 5.退出菜单")
                    if choice3 == "4":
                        back_flag = True
                        break
                    elif choice3 == "5":
                        quit_flag = True
                        break
                    elif choice3 == "1":
                        add_county = input("请输入添加的区县:")
                        a[choice][choice2].append(add_county)
                    elif choice3 == "2":
                        del_county = input("请输入要删除的区县:")
                        a[choice][choice2].remove(del_county)
                    elif choice3 == "3":
                        modify_county = input("请输入要修改的区县:")
                        modified_county = input("%s修改为:" % modify_county)
                        a[choice][choice2].remove(modify_county)
                        a[choice][choice2].append(modified_county)
                    else:
                        print("输入错误!")
            elif choice2 == "1":
                add_city = input("请输入添加的城市:")
                a[choice][add_city] = []
            elif choice2 == "2":
                del_city = input("请输入要删除的城市:")
                del a[choice][del_city]
            elif choice2 == "3":
                modify_city = input("请输入要修改的城市:")
                modified_city = input("%s修改为:" % modify_city)
                value = a[choice][modify_city]
                del a[choice][modify_city]
                a[choice][modified_city] = value
            else:
                print("输入错误!")
    elif choice == "1":
        add_province = input("请输入添加的省份:")
        a[add_province] = {}
    elif choice == "2":
        del_province = input("请输入要删除的省份:")
        del a[del_province]
    elif choice == "3":
        modify_province = input("请输入要修改的省份:")
        modified_province = input("%s修改为:" % modify_province)
        value = a[modify_province]
        del a[modify_province]
        a[modified_province] = value
    else:
        print("输入错误!")

b = str(a)
f_write.write(b)

f_read.close()     #关闭文件
f_write.close()

程序中还有大量重复的代码段,还需继续优化。

你可能感兴趣的:(python)