zoukankan      html  css  js  c++  java
  • Python笔记4(作业)

    文件存储格式如下:
    id,name,age,phone,job
    1,Alex,22,13651054608,IT
    2,Egon,23,13304320533,Tearcher
    3,nezha,25,1333235322,IT

    基础必做:
    a.可以进行查询,支持三种语法:
    select 列名1,列名2,… where 列名条件
    支持:大于小于等于,还要支持模糊查找。
    示例:
    select name, age where age>22
    select * where job=IT
    select * where phone like 133

    userinfo文件内容如下:

    1,Alex,22,13651054608,IT
    2,Egon,23,13304320533,Tearcher
    3,nezha,25,1333235322,IT

     1 #查询语句
     2 # select name,age where age>22
     3 # select * where job=IT
     4 # select * where phone like 133
     5 
     6 #读文件
     7 def read_file():
     8     with open('userinfo','r') as f:
     9         for line in f:
    10             line_lst = line.strip().split(',')
    11             yield line_lst
    12 
    13 #筛选
    14 def filter_item(col,value,condition):
    15     dic = {'id':0,'name':1,'age':2,'phone':3,'job':4}
    16     correct = []#存储符合条件的行
    17     '''筛选条件'''
    18     for line_lst in read_file():
    19         if eval(condition):
    20             correct.append((line_lst))
    21     return correct
    22 
    23 
    24 #分析条件
    25 def condition_an(con):
    26     # 分析条件
    27     #根据条件中的符号来进行操作:> = < like
    28     if '>' in con:
    29         col_name,value = con.split('>')
    30         condition = 'int(line_lst[dic[col]]) > int(value)'
    31     elif '<' in con:
    32         col_name,value = con.split('<')
    33         condition = 'int(line_lst[dic[col]]) < int(value)'
    34     elif '=' in con:
    35         col_name,value = con.split('=')
    36         condition = 'line_lst[dic[col]] == value'
    37     elif 'like' in con:
    38         col_name,value = con.split('like')
    39         condition = 'value in line_lst[dic[col]]'
    40     correct = filter_item(col_name.strip(),value.strip(),condition)
    41     return correct
    42 
    43 
    44 #输出结果
    45 def show(col,correct):
    46     dic = {'id': 0, 'name': 1, 'age': 2, 'phone': 3, 'job': 4}
    47     result_list_final = []
    48     if '*' == col.strip():
    49         col_lst = dic.keys()
    50         result_list_final.append(col_lst)
    51     else:
    52         col_lst = col.split(',')
    53         result_list_final.append(col_lst)
    54     for i in correct:
    55         result_list = []
    56         for col in col_lst:
    57             result_list.append(i[dic[col]])
    58         result_list_final.append(result_list)
    59     return result_list_final
    60 
    61 #结果数据展示
    62 def information(result):
    63     len1 = len(result[0])
    64     print('+','result'.center(len1*14-3,'-'),'+')
    65     for i in result:
    66         print(end='|')
    67         if result.index(i)==0:
    68             for j in i:
    69                 print(j.center(13,' '),end='|')
    70             print()
    71             print('+','-'*(len1*14-3),'+')
    72         else:
    73             for j in i:
    74                 print(j.center(13,' '),end='|')
    75             print()
    76     print('+','end'.center(len1*14-3, '-'),'+')
    77 
    78 flag=True
    79 while flag:
    80     select = input('请按规则输入要查询的语句,退出程序请输入Q:')
    81     if select == 'Q':
    82         flag=False
    83     elif select.find('select')<0 or select.find('where')<0:
    84         print('语法错误')
    85     else:
    86         col,con = select.split('where') # 要显示的列,条件
    87         col = col.replace('select','').strip()
    88         correct = condition_an(con)#调用分析条件的函数
    89         result=show(col,correct)#调用输出结果的函数
    90         information(result)#调用结果数据展示函数
  • 相关阅读:
    记一次开发的日常2020-01-09
    python configparser模块
    python logging模块
    python eval() hasattr() getattr() setattr() 函数使用方法详解
    redis 连接池
    Python 数据库连接池
    Object arrays cannot be loaded when allow_pickle=False
    注册网站 captcha reCHAPTCHA 错误
    网站收藏
    Python创建命令行应用的工具 tools for command line application in python
  • 原文地址:https://www.cnblogs.com/xingye-mdd/p/9017093.html
Copyright © 2011-2022 走看看