# 员工信息表: 完善代码,背下来给代码加注释
column_dic = {'id': 0, 'name': 1, 'age': 2, 'phone': 3, 'job': 4} # 将文件每一列的名字和数字的对应关系
# --条件--筛选
def filter_handler(operate, col): # >, age>22
"""
精心筛选工作
:param opertate: 用户要进行的操作是><= like
:param col: 用户输入的where条件
:return: 被选中的所有行组成的列表,其中每一行都是一个列表
"""
select_lst = [] # 被选中的列表
col, val = col.split(operate) # age,22='>','age>22'
col = col.strip() # age
val = val.strip() # 22
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('file', encoding='utf-8')
for line in f:
line_lst = line.strip().split(',') # [id, name, 25, phone, job]
if eval(judge):
select_lst.append(line_lst)
f.close()
return select_lst
# 展示符合条件的员工信息
def get_selected_line(con): # con = 'age>22'
"""
获取所有要查找的行,并将每一行作为一个列表项存储在select_lst中
:param con:
:return: 存储了符合条件的行的列表
"""
if '>' in con:
selected_lst = filter_handler('>', con)
elif '<' in con:
selected_lst = filter_handler('<', con)
elif '=' in con:
selected_lst = filter_handler('==', con.replace('=', '=='))
elif 'like' in con:
selected_lst = filter_handler('in', con)
return selected_lst
# 读取文件 ---将文件中的内容整理
def get_show_lst(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(col_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') # ['select name, age', 'age>22']
con = ret[1].strip() # 'age>22'
# 根据select条件解析用户需要展示的内容
show_lst = get_show_lst(ret[0]) # '[NAME,AGE]' = 'select name,age'
# 根据where条件解析用户想查找的内容
select_lst = get_selected_line(con) # selected_lst中存储了所有符合条件的内容
# 将符合条件的内容按照用户的需求展示出来
show(select_lst, show_lst) # 显示

文件查询 select name,age where age>22的更多相关文章

  1. Oracle 查询(SELECT)语句(一)

    Ø  简介 本文介绍 Oracle 中查询(SELECT)语句的使用,在 SQL 中 SELECT 语句是相对内容较多的,也是相对比较复杂一点的,所以这里拿出来单独学习. 首先,我们先来理一下思路,我 ...

  2. mysql 查询select语句汇总

    数据准备: 创建表: create table students( id int unsigned primary key auto_increment not null, name varchar( ...

  3. Mybatis实体类的映射文件中select,insert语句使用

    id:在命名空间中唯一的标识符,可以被用来引用这条语句. parameterType:设置传入这条语句的参数的数据类型,如int,String...... resultType:设置从这条语句中返回数 ...

  4. [刘阳Java]_MyBatis_映射文件的select标签入门_第3讲

    1.Mybatis映射文件的<select>标签主要帮助我们完成SQL语句查询功能,<select>标签它包含了很多属性,下面简单对<select>标签的属性做一个 ...

  5. SqlServer:SqlServer(xpath,字段约束,时间查询,数据文件查询及还原,压缩备份)

    1.xpath --1.文档 select @data --2.任意级别是否存在price节点 select @data.exist('//price') --3.获取所有book节点 select ...

  6. Java-MyBatis-MyBatis3-XML映射文件:select

    ylbtech-Java-MyBatis-MyBatis3-XML映射文件:select 1.返回顶部 1. select 查询语句是 MyBatis 中最常用的元素之一,光能把数据存到数据库中价值并 ...

  7. sql_查询select

    sql_查询select   /****** Script for SelectTopNRows command from SSMS ******/ [r_gonghao] ,[r_mingzi] , ...

  8. 查询select

    --------------select查询-------------------查询所有信息(方法一)select * from stuinfo --*号代表所有列--查询所有信息(方法二)sele ...

  9. mysql数据库(二):查询(SELECT)

    一. 数据库查询—查询(SELECT) 单表查询 多表联合查询 二. 查询—单表查询 查询特定字段: select <字段1,字段2,...> from <表名>; 示例:查询 ...

随机推荐

  1. C# Dictionary(字典)源码解析&效率分析

    通过查阅网上相关资料和查看微软源码,我对Dictionary有了更深的理解. Dictionary,翻译为中文是字典,通过查看源码发现,它真的内部结构真的和平时用的字典思想一样. 我们平时用的字典主要 ...

  2. C - dlopen dlsym

    -----------------------------------------------------------------------------dlsym------------------ ...

  3. zoj-3872 Beauty of Array (dp)

    ]Edward has an array A with N integers. He defines the beauty of an array as the summation of all di ...

  4. hihocoder 1631

    时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 There are many homeless cats in PKU campus. They are all happy ...

  5. Netty(二)Netty 与 NIO 之前世今生

    2.1 Java NIO 三件套 在 NIO 中有几个核心对象需要掌握:缓冲区(Buffer).选择器(Selector).通道(Channel). 2.1.1 缓冲区 Buffer 1.Buffer ...

  6. awesome youtube programming video tutorials

    awesome youtube programming video tutorials youtube programming tutorials https://www.youtube.com/fe ...

  7. PPT & order & animation

    PPT & order & animation powerpoint order of appearance https://support.office.com/en-us/arti ...

  8. Raspberry Pi & Raspberry Pi 4

    Raspberry Pi & Raspberry Pi 4 pdf https://www.raspberrypi.org/magpi/issues/beginners-guide-2nd-e ...

  9. API protocols All In One

    API protocols All In One SOAP vs. REST vs. JSON-RPC vs. gRPC vs. GraphQL vs. Thrift https://www.mert ...

  10. 金三银四助力面试-手把手轻松读懂HashMap源码

    前言 HashMap 对每一个学习 Java 的人来说熟悉的不能再熟悉了,然而就是这么一个熟悉的东西,真正深入到源码层面却有许多值的学习和思考的地方,现在就让我们一起来探索一下 HashMap 的源码 ...