The content of this article is about how Python reads the multi-layer menu (code) of yaml files. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Need to use Python knowledge points
Python’s object attribute method;
Use the dictionary {key: value} to extract the value;
Add the list;
The use of if loop combined with break;
yaml file reading;
The code is as follows:
#!/usr/bin/python34 import sys,os,re,yaml,time #reload(sys) #sys.setdefaultencoding('utf-8') ######################对input输入字符类型判断并转化##################### def input_handle(s): if str.isdigit(s): ###对输入是否是数字进行判断 s = int(s) ###如果输出的是个数字,则转化为整数类型 return s # try: # eval(s) ###eval将字符串str当成有效的表达式来求值并返回计算结果 # except NameError: ###名称错误 # return s # except SyntaxError: ###语法错误 # return s # else: # return eval(s) ####################框架函数###################### def framework(province='',city='',county=''): os.system('cls') ###清屏### print(''' ****************************************************************** * * * 欢迎访问全国省市查询系统 * * * ****************************************************************** +----------------------------------------------------------------- | 省份: %s | | 市(区): %s | | 县(区): %s +----------------------------------------------------------------- ''' % (province,city,county)) ######################输出展示函数################# def show(province_name='',city_name='',county_name=''): output= ''' ****************************************************************** * * 美丽的%s %s %s * 欢迎您 * 这里的山美,水美,妹子更美 * * ****************************************************************** ''' print(output % (province_name,city_name,county_name)) ###################菜单第一层省份或直辖市输出函数################# def province_show(province_list): ############申明全局变量#################### global P_NAME global C_NAME global X_NAME global FLAG_M province_dict = {} ############对省份或直辖市列表参数进行遍历并加上数字编号############### for k,v in enumerate(province_list,1): province_dict[k] = v print('%d . %s' % (k,v) + '\t',end='') ###加上end参数,取消默认换行### if k % 4 == 0: ###按4列换行### print() print('\n================================================================') print('q : Exit') ###############键盘读入编号或省份,可以输入汉字################# province_index = input('请输入编号或省份 : ') ###############如果输入非空,对输入进行判断并转化类型########### if len(province_index) != 0: province_index = input_handle(province_index) if province_index == 'q': ###如果输入为q,则退出程序### sys.exit(0) elif province_index in province_dict.keys(): ###如果输入为数字编号,则从字典中获取具体省份或直辖市名称### P_NAME = province_dict[province_index] ###对全局变量赋值省份名称### elif province_index in province_dict.values(): ###如果输入为具体省份,则从字典中获取具体省份或直辖市名称### P_NAME = province_index ###对全局变量赋值省份名称### else: P_NAME = '' ###输入其他字符,赋值全局变量为空### while P_NAME: ###全局变量不为空进行循环### framework(P_NAME,C_NAME,X_NAME) ###调用框架### if type(yaml_dict[P_NAME]) is list: city_show(P_NAME) ###调用城市函数,并传入省份值### if FLAG_M == 'b': ###城市函数输入b,返回上一层,重新选择省份### break else: show(P_NAME) ###调用输出展示函数### time.sleep(5) P_NAME = '' break else: print('输入错误,请重新输入!') ###P_NAME为空,即输入错误,重新输入### time.sleep(2) ##############菜单第二层城市输出函数####################### def city_show(province_name): ############申明全局变量############### global P_NAME global C_NAME global X_NAME global FLAG_M city_name = '' ###定义城市变量默认为空### city_list = yaml_dict[province_name] ###定义赋值城市列表### city_dict = {} ###定义城市编号和名称字典### city_county_dict = {} ###定义地级市和下属区县字典### ############对城市列表参数进行遍历并加上数字编号############### for k,v in enumerate(city_list,1): if type(v) is str: ###直辖市只有二层菜单,第二层为直接的各区或县,值类型为具体字符### city_dict[k] = v ###对直辖市下的区或县进行新字典赋值,方便查询和展示### print('%d . %s' % (k,v) + '\t',end='') ###末尾加',',取消默认换行### elif type(v) is dict: ###其他省份有三层菜单,第二层为各地级市,值类型字典### for kk,vv in v.items(): ###对地级市的字典进行遍历### city_dict[k] = kk ###对其他省份下的地级市进行新字典赋值,方便查询和展示### city_county_dict[kk] = vv ###对二层地级市和三层县市重新赋值新字典### print('%d . %s' % (k,kk) + '\t',end='') ###加上end参数,取消默认换行### else: pass if k % 4 == 0: ###按4列换行### print() print('\n================================================================') print('q : Exit b : Back') ###############键盘读入编号或区市,可以输入汉字################# city_index = input('请输入编号或区市 : ') ###############如果输入非空,对输入进行判断并转化类型########### if len(city_index) != 0: city_index = input_handle(city_index) if city_index == 'q': ###如果输入为q,则退出程序### sys.exit(0) elif city_index == 'b': ###如果输入为b,则返回上一层,重新选择省份或直辖市### (P_NAME,C_NAME,FLAG_M) = ('','','b') ###全局变量P_NAME,C_NAME设置为空,FLAG_M设置为b,则返回上一层### return ###直接返回,不进行函数以下的操作### elif city_index in city_dict.keys(): ###如果输入为数字编号,则从字典中获取具体城市名称### city_name = city_dict[city_index] ###赋值地级市的名称,并对全局变量进行赋值### (P_NAME,C_NAME,FLAG_M) = (province_name,city_name,'') elif city_index in city_dict.values(): ###如果输入为城市名称,则从字典中获取具体省份名称### city_name = city_index ###赋值地级市的名称,并对全局变量进行赋值### (P_NAME,C_NAME,FLAG_M) = (province_name,city_name,'') else: pass ###如果输入其他字符,则不做任何操作### if city_name: ###如果地级市名字不为空,即键盘输入为要求字符### if city_name in city_county_dict.keys(): ###判断是省份的地级市名字### while C_NAME: ###环境变量C_NAME不为空### framework(P_NAME,C_NAME,X_NAME) ###调用框架函数,并将省份名字和地级市名字传入### ###调用三层区县显示函数,并传入具体变量### county_show(P_NAME,C_NAME,city_county_dict[city_name]) if FLAG_N == 'b': ###三层区县函数输入b,返回上一层,重新选择地级市### break else: print('输入错误,请重新输入!') ###C_NAME为空,即输入错误,重新输入### time.sleep(2) else: ###判断是直辖市的区或县名字### show(P_NAME,C_NAME) ###调用输出展示函数### time.sleep(5) else: ###输入非要求字符,提示重新输入### print('输入错误,请重新输入!') time.sleep(2) ##############菜单第三层区县输出函数####################### def county_show(province_name,city_name,county_list): ############申明全局变量#################### global P_NAME global C_NAME global X_NAME global FLAG_N county_name = '' ###定义三级区县变量默认为空### county_dict = {} ##定义赋值区县字典### ############对区县列表参数进行遍历并加上数字编号############### for k,v in enumerate(county_list,1): if type(v) is str: ###第三层为直接的各区或县,值类型为具体字符str### county_dict[k] = v ###对区或县进行新字典赋值,方便查询和展示### print('%d . %s' % (k,v) + '\t',end='') ###加上end参数,取消默认换行### if k % 4 == 0: ###按4列换行### print() print('\n================================================================') print('q : Exit b : Back') ###############键盘读入编号或区县,可以输入汉字################# county_index = input('请输入编号或区县 : ') ###############如果输入非空,对输入进行判断并转化类型########### if len(county_index) != 0: county_index = input_handle(county_index) if county_index == 'q': ###如果输入为q,则退出程序### sys.exit(0) elif county_index == 'b': ###如果输入为b,则返回上一层,重新选择第二层地级市### (P_NAME,C_NAME,X_NAME,FLAG_N) = (province_name,'','','b') ###全局变量C_NAME设置为空,FLAG_M设置为b,则返回上一层### return ###直接返回,不进行函数以下的操作### elif county_index in county_dict.keys(): ###如果输入为数字编号,则从字典中获取具体区县名称### county_name = county_dict[county_index] ###赋值区县的名称,并对全局变量进行赋值### (P_NAME,C_NAME,X_NAME,FLAG_N) = (province_name,city_name,county_name,'') elif county_index in county_dict.values(): ###如果输入为区县名称,则从字典中获取具体区县名称### county_name = county_index ###赋值区县的名称,并对全局变量进行赋值### (P_NAME,C_NAME,X_NAME,FLAG_N) = (province_name,city_name,county_name,'') else: ###如果输入其他字符,则不做任何操作### (P_NAME,C_NAME,X_NAME,FLAG_N) = (province_name,city_name,'','') if county_name: ###如果区县名字不为空,即键盘输入为要求字符### show(P_NAME,C_NAME,X_NAME) ###调用输出展示函数### time.sleep(5) else: print('输入错误,请重新输入!') ###输入非要求字符,提示重新输入### time.sleep(2) ##########################################主程序############################### ###############读取yaml格式文件####################### fd = open(r'G:\sicps\test\pro',encoding='utf-8') yaml_dict = yaml.load(fd) ###############定义全局变量########################### P_NAME = '' ###省份或直辖市全局变量### C_NAME = '' ###各省地级市或直辖市区县的全局变量### X_NAME = '' ###各省地级市下的区或县 全局变量### FLAG_M = '' ###退出菜单第二层,返回上一层循环的变量### FLAG_N = '' ###退出菜单第三层,返回上一层循环的变量### ###############获取省份或直辖市的列表################# prov_list = yaml_dict.keys() ###############主循环开始############################# while True: framework(P_NAME,C_NAME,X_NAME) ###调用框架函数,显示初始状态### province_show(prov_list) ###调用第一层省份或直辖市输出函数###
The above is the detailed content of How Python reads yaml file multi-layer menu (code). For more information, please follow other related articles on the PHP Chinese website!

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.

Python's real-world applications include data analytics, web development, artificial intelligence and automation. 1) In data analysis, Python uses Pandas and Matplotlib to process and visualize data. 2) In web development, Django and Flask frameworks simplify the creation of web applications. 3) In the field of artificial intelligence, TensorFlow and PyTorch are used to build and train models. 4) In terms of automation, Python scripts can be used for tasks such as copying files.

Python is widely used in data science, web development and automation scripting fields. 1) In data science, Python simplifies data processing and analysis through libraries such as NumPy and Pandas. 2) In web development, the Django and Flask frameworks enable developers to quickly build applications. 3) In automated scripts, Python's simplicity and standard library make it ideal.

Python's flexibility is reflected in multi-paradigm support and dynamic type systems, while ease of use comes from a simple syntax and rich standard library. 1. Flexibility: Supports object-oriented, functional and procedural programming, and dynamic type systems improve development efficiency. 2. Ease of use: The grammar is close to natural language, the standard library covers a wide range of functions, and simplifies the development process.

Python is highly favored for its simplicity and power, suitable for all needs from beginners to advanced developers. Its versatility is reflected in: 1) Easy to learn and use, simple syntax; 2) Rich libraries and frameworks, such as NumPy, Pandas, etc.; 3) Cross-platform support, which can be run on a variety of operating systems; 4) Suitable for scripting and automation tasks to improve work efficiency.

Yes, learn Python in two hours a day. 1. Develop a reasonable study plan, 2. Select the right learning resources, 3. Consolidate the knowledge learned through practice. These steps can help you master Python in a short time.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.