三级菜单读取文件版

ext_flag = False
# file = open("menu",'r',encoding='utf-8')    # 打开3级菜单文本文件
# f = file.read()

with open('menu','r',encoding='utf-8') as aff:  # 打开3级菜单文本文件
    f= aff.read()
for line in f:          # 读取文件中每行信息
    file_str = str(f)    # 将每行信息转成字符串格式
now_layers = eval(file_str)    # 字符串转成字典格式
last_layers = [now_layers]      #把字典转成列表
# print(now_layers)
# print(last_layers)

while not ext_flag:
    for key in now_layers:
        print(key)
    choice = input('>>:').strip()
    if len(choice) == 0: continue
    if choice in now_layers:
        last_layers.append(now_layers)
        now_layers =now_layers[choice]
    if choice =='b':
        if last_layers:
            now_layers=last_layers[-1]
            last_layers.pop()
    if choice == 'q':
        ext_flag = True

menu文件内容

 

{
'浙江':{
'杭州':{
'西湖':{
'西溪',
'三墩'},
'滨江':{'滨和':{}, '高新':{}}
},
'丽水':{
'龙泉':{'宝剑':{}, '瓷器':{}},
'青田':{'石雕':{}, '华侨':{}},
'缙云':{'烧饼':{}, '木雕':{}},
},
},
'山东':{
'烟台':{'大海':{}, '日照':{}},
'青岛':{'平度':{}, '黄岛':{}},
'东营':{'利津', '垦利'}
},
'广东':{
'深圳':{'南山':{}, '福田':{}},
'梅州':{'五华':{}, '兴宁':{}},
'佛山':{'三水':{}, '桂城':{}}
}
}

posted @ 2017-09-14 16:29  天涯客2017  阅读(269)  评论(0编辑  收藏  举报