python學習筆記(26) 員工信息表
阿新 • • 發佈:2018-11-04
con sel print pytho 內容 opera enc 輸入 pen
user:
1,aaa,11,mi1,miboy
2,bbb,22,mi2,miboy
3,ccc,33,mi3,miboy
4,ddd,44,mi4,miboy
column_dic = {‘id‘:0,‘name‘:1,‘age‘:2,‘phone‘:3,‘job‘:4}
def filter_handler(operate,con):
‘‘‘
進行篩選工作
:param operate:用戶輸入的操作
:param con:用戶輸入的條件
:return:被選中的所有行組成的列表,每一個都是一個列表
‘‘‘
selected_lst = []
col,val = con.split(operate)
col = col.strip()
val = val.strip()
judge = ‘int(line_lst[column_dic[col]]) %s int(val)‘%operate if operate == ‘>‘ or operate == ‘<‘ else ‘line_lst[column_dic[col]]%s val‘%operate
f = open(‘users‘,encoding=‘utf-8‘)
for line in f:
line_lst = line.strip().split(‘,‘)
if eval(judge):
selected_lst.append(line_lst)
f.close()
return selected_lst
def get_selected_line(con):
‘‘‘
獲取要查找的行,並將每一行作為一個列表存儲在selected_list中
:param con:用戶輸入的查找條件
:return:符合條件的行的列表
‘‘‘
if ‘>‘ in con:
selected_list = filter_handler(‘>‘,con)
elif‘<‘ in con:
selected_list = filter_handler(‘<‘,con)
elif‘=‘ in con:
selected_list = filter_handler(‘==‘,con.replace(‘=‘,‘==‘))
elif ‘like‘ in con:
selected_list = filter_handler(‘in‘,con)
return selected_list
def get_show_list(col_condition):
‘‘‘
獲取要展示的列名
:param col_condition: 用戶輸入的select條件
:return:列名組成的字典
‘‘‘
col_info_lst = col_condition.strip().split(‘select‘)
col_info_lst = [col_info_item for col_info_item in col_info_lst if col_info_item.strip()]
if col_info_lst:
col_info = col_info_lst[0].strip()
if col_info == ‘*‘:
return column_dic.keys()
elif col_info:
ret = col_info.split(‘,‘)
return [item.strip() for item in ret]
else:print(con_info)
def show(selected_lst,show_lst):
‘‘‘
展示符合條件的內容
:param selected_lst: 符合條件的行的列表
:param show_lst: 所有要展示的字段
:return: None
‘‘‘
for selected_item in selected_lst:
for col in show_lst:
print(selected_item[column_dic[col]],end = ‘ ‘)
print() #打印回車
condition = input(‘請輸入:‘)
ret = condition.split(‘where‘)
con = ret[1].strip()
show_lst = get_show_list(ret[0])
selected_lst = get_selected_line(con)
show(selected_lst,show_lst)
python學習筆記(26) 員工信息表