#shur=selecet name,age where age>23
user_input=input('请输入你的选则')
dic={'id':0,'name':1,"age":2,"phone":3,"job":4}
#user_input='celecet name,age where age>23'
to_s=user_input.strip('celecet')
to_s=to_s.strip()
to_show,condit=to_s.split('where')#可以用两个变量去接受split切出来的内容
to_show=to_show.strip()#能在循环外去空格等操作尽量在循环外去空格,
# 否则循环里如果多次调用此变量,需要多次修改才能行
condit=condit.strip()
lis = []
def cond(condit):
if ">" in condit:
chaifen_cond=condit.split('>')
with open('name_list', "r", encoding='utf-8') as f:#打开文件的操作要在
# 循环里,因为文件中有多行,需要执行完整for循环才能读完
for line in f:
line_lis = line.split(',')
if int(line_lis[dic[chaifen_cond[0]]]) > int(chaifen_cond[1]):
lis.append(line_lis)
elif "<" in condit:
chaifen_cond=condit.split('<')
with open('name_list', "r", encoding='utf-8') as f:
for line in f:
line_lis = line.split(',')
if int(line_lis[dic[chaifen_cond[0]]]) < int(chaifen_cond[1]):
lis.append(line_lis)
elif "=" in condit:
chaifen_cond=condit.split('=')
with open('name_list', "r", encoding='utf-8') as f:
for line in f:
line_lis = line.split(',')
if (chaifen_cond[1]) in (line_lis[dic[chaifen_cond[0]]]):
lis.append(line_lis)
elif "like" in condit:
chaifen_cond = condit.split('like')
with open('name_list', "r", encoding='utf-8') as f:
for line in f:
line_lis = line.split(',')
if (chaifen_cond[1]) in (line_lis[dic[chaifen_cond[0]]]):
lis.append(line_lis)
return lis
g=cond(condit)
#print(g)
def cond1(g):
for i in g:
v = ''
to_to_show = to_show.split(',')
for k in to_to_show:
v+=' %s'%i[dic[k]]
print(v)
cond1(g)