• forth作业--sql查询


     1 '''
     2 注意
     3 1.注意先切分后去空格
     4 2.注意打印空
     5 3.int比较
     6 4.精彩  生成器函数
     7 这个版本必须会写!
     8 
     9 '''
    10 
    11 def read_file():
    12     '''读文件'''
    13     with open('userinfo','r') as f:
    14         for line in f:
    15             line_lst = line.strip().split(',')  # ['1','Alex','22','13651054608','IT']
    16             yield line_lst
    17 
    18 def filter_item(col,value,signal):
    19     '''筛选条件  col='age' 22 >'''   #dic[col] == dic['age'] == 2
    20     dic = {'id':0,'name':1,'age':2,'phone':3,'job':4}
    21     correct = []   #存储符合条件的行列表
    22     for line_lst in read_file():
    23         #['1','Alex','22','13651054608','IT']   age > 20
    24         if signal == '>':
    25             if int(line_lst[dic[col]]) > int(value):    #3.int比较
    26                 correct.append(line_lst)
    27         elif signal == '<':
    28             if int(line_lst[dic[col]]) < int(value):
    29                 correct.append(line_lst)
    30         elif signal == '=':
    31             if line_lst[dic[col]] == value:
    32                 correct.append(line_lst)
    33         elif signal == 'like':
    34             if value in line_lst[dic[col]]:
    35                 correct.append(line_lst)
    36     return correct
    37 
    38 def condition_an(con):
    39     # 分析条件
    40     # 根据条件中的符号来进行操作: > < = like
    41     if '>' in con:   # age > 22
    42         col_name,value = con.split('>')   #age,22
    43         col_name = col_name.strip()  #'age'      1.注意先切分后去空格
    44         value = value.strip()        #'22'
    45         correct = filter_item(col_name,value,'>')  # 筛选出来所有符合条件的项
    46     elif '<' in con:   # age < 22
    47         col_name,value = con.split('<')   #age,22
    48         col_name = col_name.strip()  #'age'
    49         value = value.strip()        #'22'
    50         correct = filter_item(col_name,value,'<')  # 筛选出来所有符合条件的项
    51     elif '=' in con:   # age < 22
    52         col_name,value = con.split('=')   #age,22
    53         col_name = col_name.strip()  #'age'
    54         value = value.strip()        #'22'
    55         correct = filter_item(col_name,value,'=')  # 筛选出来所有符合条件的项
    56     elif 'like' in con:   # age < 22
    57         col_name,value = con.split('like')   #age,22
    58         col_name = col_name.strip()  #'age'
    59         value = value.strip()        #'22'
    60         correct = filter_item(col_name,value,'like')  # 筛选出来所有符合条件的项
    61     return correct
    62 
    63 def show(col,correct):
    64     # 展示符合条件的行中需要的字段  col = 'name,age,phone,job'   '*'
    65     dic = {'id': 0, 'name': 1, 'age': 2, 'phone': 3, 'job': 4}
    66     if '*' == col.strip():
    67         col_lst = dic.keys()      #[id,name,age,phone,job]
    68     else:
    69         col_lst = col.split(',')  #[name,age,phone,job]
    70     for i in correct:
    71         #i = ['1', 'Alex', '22', '13651054608', 'IT']
    72         for col in col_lst:
    73             print(i[dic[col]],end=' ')
    74         print()                  #2.注意打印空
    75 # exp = input('>>>')
    76 exp = 'select * where phone like 133'
    77 # exp = 'select * where age>22'
    78 col,con = exp.split('where')  # 要显示的列,条件
    79 col = col.replace('select','').strip()
    80 print(col,con)
    81 correct = condition_an(con)   #调用分析条件的函数
    82 show(col,correct)
     1 def read_file():
     2     '''读文件'''
     3     with open('userinfo','r') as f:
     4         for line in f:
     5             line_lst = line.strip().split(',')  # ['1','Alex','22','13651054608','IT']
     6             yield line_lst
     7 
     8 def filter_item(col,value,condition):
     9     '''筛选条件  
    10     col='age' 
    11     value = '22'
    12     condition =  'int(line_lst[dic[col]]) > int(value)'
    13     '''
    14     dic = {'id':0,'name':1,'age':2,'phone':3,'job':4}
    15     correct = []   #存储符合条件的行列表
    16     for line_lst in read_file():
    17         #line_lst = ['1','Alex','22','13651054608','IT']   age > 20
    18         if eval(condition):                 #1.最神奇的语句!eval用法,条件返回语句
    19             correct.append(line_lst)
    20     return correct
    21 
    22 def condition_an(con):
    23     # 分析条件
    24     # 根据条件中的符号来进行操作: > < = like
    25     if '>' in con:   # age > 22
    26         col_name,value = con.split('>')   #age,22
    27         condition = 'int(line_lst[dic[col]]) > int(value)'
    28     elif '<' in con:   # age < 22
    29         col_name,value = con.split('<')   #age,22
    30         condition = 'int(line_lst[dic[col]]) < int(value)'  # 筛选出来所有符合条件的项
    31     elif '=' in con:   # age < 22
    32         col_name,value = con.split('=')   #age,22
    33         condition = 'line_lst[dic[col]] == value'  # 筛选出来所有符合条件的项
    34     elif 'like' in con:   # age < 22
    35         col_name,value = con.split('like')   #age,22
    36         condition = 'value in line_lst[dic[col]]'   # 筛选出来所有符合条件的项
    37     correct = filter_item(col_name.strip(), value.strip(), condition)  # 筛选出来所有符合条件的项
    38     return correct
    39 
    40 def show(col,correct):
    41     # 展示符合条件的行中需要的字段  col = 'name,age,phone,job'  | col = '*'
    42     dic = {'id': 0, 'name': 1, 'age': 2, 'phone': 3, 'job': 4}
    43     if '*' == col.strip():
    44         col_lst = dic.keys()      #[id,name,age,phone,job]
    45     else:
    46         col_lst = col.split(',')  #[name,age,phone,job]
    47     for i in correct:
    48         #i = ['1', 'Alex', '22', '13651054608', 'IT']
    49         for col in col_lst:
    50             print(i[dic[col]],end=' ')
    51         print()
    52 # exp = input('>>>')
    53 # exp = 'select * where phone like 133'
    54 exp = 'select * where age>22'
    55 col,con = exp.split('where')  # 要显示的列,条件
    56 # col= 'select * '      con= ' age>22'
    57 # col= 'select name,age '      con= ' age>22'
    58 col = col.replace('select','').strip()   # *  |  name,age,job...
    59 print(col,con)
    60 correct = condition_an(con)   #调用分析条件的函数
    61 show(col,correct)             #调用展示结果的函数
    core-进阶
  • 相关阅读:
    NET打包時加入卸载功能
    c#水晶报表注册码
    sqlserver:某年份某月份 是否在某时间段内的函数
    修改KindEditor编辑器 版本3.5.1
    Flash大文件上传(带进度条)
    让.Net程序脱离.net framework框架运行的方法(转载)
    夏天到了,什么时候园子的T恤可以出来?
    VS2005项目的安装与布署
    解决Select覆盖Div的简单直接的方法
    .Net向Page和UpdatePanel输出JS
  • 原文地址:https://www.cnblogs.com/lijie123/p/9022347.html
Copyright © 2020-2023  润新知