dic = {'id': 0, 'name': 1, 'age': 2, 'phone': 3, 'job': 4} def add_info(filename): user_info = [] with open(filename, 'r', encoding='utf-8') as f: lines = f.readlines() last_line = lines[-1] id_max = last_line[dic['id']] IF id_max.isdigit (): ID = int (id_max) +. 1 user_info.append (STR (ID)) name = INPUT ( ' Enter your name: ' ) .strip () IF name.isspace (): Print ( ' name can not be empty ' ) the else : user_info.append (name) Age = the iNPUT ( ' Please enter ages: ' ) .strip () IF age.isspace (): Print ( ' Age can not be empty ' ) elif notage.isdigit (): Print ( ' ages must be numeric ' ) the else : user_info.append (Age) Phone = the INPUT ( ' Please enter the phone: ' ) .strip () IF phone.isspace (): Print ( ' phone can not empty ' ) elif not phone.isdigit (): Print ( ' phone numbers must consist of all ' ) the else : user_info.append (phone) the job = the iNPUT ( ' Please enter the work:' ) .Strip () IF job.isspace (): Print ( ' work can not be empty ' ) the else : user_info.append (Job) with Open (filename, ' A ' , encoding = ' UTF-. 8 ' ) AS F: info = ' , ' .join (USER_INFO) IF f.writable (): IF f.write (info)> 0: Print ( ' user [% s] information added successfully! ' % name) DEF del_info(): pass def read_file(filename): with open(filename, encoding='utf-8') as f: for line in f: line = line.strip() line_list = line.split(',') yield line_list def cond_filter(condition, filename): condition = condition.strip() if '>=' in condition: condition = condition.split('>=') col = condition[0].strip() val = condition[1].strip() g = read_file(filename) for line_list in g: if int(line_list[dic[col]]) >= int(val): yield line_list elif '<=' in condition: condition = condition.split('<=') col = condition[0].strip() val = condition[1].strip() g = read_file(filename) for line_list in g: if int(line_list[dic[col]]) <= int(val): yield line_list elif '>' in condition: condition = condition.split('>') col = condition[0].strip() val = condition[1].strip() g = read_file(filename) for line_list in g: if int(line_list[dic[col]]) > int(val): yield line_list elif '<' in condition: condition = condition.split('<') col = condition[0].strip() val = condition[1].strip() g = read_file(filename) for line_list in g: if int(line_list[dic[col]]) < int(val): yield line_list elif 'like' in condition: condition = condition.split('like') col = condition[0].strip() val = condition[1].strip() g = read_file(filename) for line_list in g: if val in line_list[dic[col]]: yield line_list else: print('查询暂时只支持>=、<=、>、<、like') def print_info(col_lst, staff): if ' * ' In col_lst: col_lst = dic.keys () for I in col_lst: Print (I, End = ' \ T \ T \ T ' ) Print ( '' ) for staff_info in Staff: for I in col_lst: Print ( staff_info [dic [i.strip ()]], End = ' \ t \ t \ t ' ) Print ( '' ) Print ( " Please select your function: [1] [2] new user information delete user information [3] query user information [exit] to exit") choose = input("请选择你的功能:").strip() while True: ret = input(">>>").lower() if 'exit'.lower() == ret.strip(): break if choose == '3': if 'where' in ret: select, where = ret.split('where') select, table = select.split('from') cols = select.replace('select', '').strip() cols = cols.split(',') g = cond_filter(where.strip(), table.strip()) print_info(cols, g) else: select, table = ret.split('from') cols = select.replace('select', '').strip() cols = cols.split(',') g = read_file(table.strip()) print_info(cols, g) ret = input(">>>") elif choose == '1': add_info('userinfo') ret = input(">>>")
Regular expressions work - Calculator
Guess you like
Origin www.cnblogs.com/Ryan-Fei/p/12214107.html
Recommended
Ranking