标签:text ali hone handle item center sam 打印 手机
可进行模糊查询
find name,age from staff_table where age > 22
find name from staff_table where dept = "IT"
find name from staff_table where id=4
find age from staff_table where name = "Alex Li"
find * from staff_table where name = "Alex Li"
find name,age,phone from staff_table where enroll_date like "2013-04-01"
可创建新员工纪录,以phone做唯一键(即不允许表里有手机号重复的情况),sta?_id自增
add staff_table Alex Li,25,134435344,IT,2015-10-29
可删除指定员工信息纪录,输入员工id,即可删除
del from staff_table where id=4
del from staff_table where age>22
可修改员工信息
UPDATE staff_table SET dept="Market" WHERE dept = "IT"
UPDATE staff_table SET age=25 WHERE name = "Alex Li"
以上每条语名执行完毕后,显示这条语句影响了多少条纪录。 比如查询语句 显示 查询出了多少条、 修改语句显示修改了多少条等。
id | name | age | phone | dept | enroll_date |
---|---|---|---|---|---|
1 | Alex Li | 22 | 13651054608 | IT | 2013-04-01 |
2 | Jack Wang | 28 | 13451024608 | HR | 2015-01-07 |
3 | Rain Wang | 21 | 13451054608 | IT | 2017-04-01 |
1,Alex Li,22,13651054608,IT,2013-04-01
2,Jack Wang,28,13451024608,HR,2015-01-07
3,Rain Wang,21,13451054608,IT,2017-04-01
4,Mack Qiao,44,15653354208,Sales,2016-02-01
5,Rachel Chen,23,13351024606,IT,2013-03-16
6,Eric Liu,19,18531054602,Marketing,2012-12-01
7,Chao Zhang,21,13235324334,Administration,2011-08-08
8,Kevin Chen,22,13151054603,Sales,2013-04-01
9,Shit Wen,20,13351024602,IT,2017-07-03
10,Shanshan Du,26,13698424612,Operation,2017-07-02
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import os
data = {} # 存放表格数据
tables = ‘‘
match = { # 表头对应关系
‘id‘: 0,
‘name‘: 1,
‘age‘: 2,
‘phone‘: 3,
‘dept‘: 4,
‘enroll_date‘: 5
}
‘‘‘
测试语句:
find name,age from staff_table where age > 22
find name from staff_table where dept = "IT"
find name from staff_table where id=4
find age from staff_table where name = "Alex Li"
find * from staff_table where name = "Alex Li"
find name,age,phone from staff_table where enroll_date like "2013-04-01"
find name,age,phone,enroll_date from staff_table where enroll_date like "2013"
add staff_table Alex Li,25,134435344,IT,2015-10-29
del from staff_table where id=4
del from staff_table where age>22
UPDATE staff_table SET dept="Market" WHERE dept = "IT"
UPDATE staff_table SET age=25 WHERE name = "Alex Li"
‘‘‘
def main():
‘‘‘
主程序
projects:需要输出的内容
tables:本次操作的表格(文件名)
rule:数据筛选的条件
:return:
‘‘‘
while True:
global tables
global data
tables = ‘‘
data = {
‘id‘: [],
‘name‘: [],
‘age‘: [],
‘phone‘: [],
‘dept‘: [],
‘enroll_date‘: []
}
inputs = input(‘>>>‘).strip()
handle = inputs.split(‘ ‘)[0]
if handle == ‘find‘:
inputs_list = inputs.split(‘ from ‘)
projects = inputs_list[0].split(‘ ‘)[1].split(‘,‘) # 打印内容
inputs_list1 = inputs_list[1].split(‘ where ‘)
tables = inputs_list1[0] # 表格
rule = inputs_list1[1].replace(‘=‘, ‘==‘)
db_open()
prints(projects,find(rule))
elif handle == ‘add‘:
inputs_list = inputs.split(‘ ‘, 2)
tables = inputs_list[1]
db_open()
add(inputs_list)
elif handle == ‘del‘:
inputs_list = inputs.split(‘ from ‘)[1].split(‘ where ‘)
tables = inputs_list[0]
rule = inputs_list[1].replace(‘=‘, ‘==‘)
db_open()
delete(find(rule))
elif handle == ‘UPDATE‘:
inputs_list = inputs.split(‘ SET ‘)
tables = inputs.split(‘ ‘)[1]
updates = inputs_list[1].split(‘ WHERE ‘)[0].replace(‘=‘, ‘==‘)
rule = inputs_list[1].split(‘ WHERE ‘)[1].replace(‘=‘, ‘==‘)
print(tables)
db_open()
update(find(rule),updates)
else:
print(‘\033[0;33mPlease input:\n‘
‘find [id,name,...] from [table] where [id,name,...][>\<\=\like][id,name,...]\n‘
‘add [table] [name,age,...]\n‘
‘del from [table] [id,name,...] where [id,name,...][>\<\=\like][id,name,...]\n‘
‘UPDATE [table] set [id,name,...] = [id,name,...] WHERE [id,name,...][>\<\=\like][id,name,...]\033[0m ‘)
continue
def db_loads(loads):
‘‘‘
将数据载入data字典,data字典用于存放表格数据
:param loads: 文件读取的每一行内容
:return:
‘‘‘
if loads:
staff_id,name,age,phone,dept,enroll_date=loads.strip().split(‘,‘)
# print(staff_id,name,age,phone,dept,enroll_date)
data[‘id‘].append(int(staff_id))
data[‘name‘].append(name)
data[‘age‘].append(int(age))
data[‘phone‘].append(int(phone))
data[‘dept‘].append(dept)
data[‘enroll_date‘].append(enroll_date)
def db_open():
‘‘‘
打开文件,读取内容,调用db_loads(loads)
:return:
‘‘‘
with open(tables,‘r‘,encoding=‘utf-8‘) as f:
for line in f:
db_loads(line.strip())
def db_save_a(data):
‘‘‘
文件追加写入
:param data:
:return:
‘‘‘
with open(tables,‘a‘,encoding=‘utf-8‘) as f:
f.write(data)
print(‘\033[0;33mdata has been saved ...\033[0m‘)
def find(rule):
‘‘‘
查找符合条件的id
:param rule: 数据筛选的条件
:return: 列表形式的id
‘‘‘
id = []
if ‘like‘ in rule:
key = rule.split(‘ ‘)
cmd = "%s in item"%(key[-1])
else:
key = re.findall(‘\w+‘, rule)
cmd = rule.replace(key[0], ‘item‘)
for index,item in enumerate(data[key[0]]):
exec(‘‘‘if %s:
id.append(index)
‘‘‘%cmd)
print(‘\033[0;33mFind %s pieces of information ...\033[0m‘%len(id))
return id
def add(inputs_list):
‘‘‘
数据增加操作,分别调用db_save_a追加内容,db_loads加载新数据至data字典
:param inputs_list:要增加的数据
:return:
‘‘‘
if int(inputs_list[2].split(‘,‘)[2]) in data[‘phone‘]:
print(‘\033[0;33mThe phone number is the same as the others...\033[0m‘)
else:
add_data = str(len(data[‘id‘])+1)+‘,‘+inputs_list[2]+‘\n‘
db_save_a(add_data)
db_loads(add_data)
def delete(lists):
‘‘‘
删除操作
:param lists: 要删除的数据
:return:
‘‘‘
with open(tables, ‘r+‘, encoding=‘utf-8‘) as f, open(‘data_new.txt‘, ‘w‘) as f2:
i,j = 1,1
for line in f:
if int(line.split(‘,‘)[0]) not in list(map(lambda x:x+1,lists)):
line = line.replace(str(j), str(i), 1)
f2.write(line)
i += 1
j += 1
os.remove(tables)
os.rename(‘data_new.txt‘,tables)
print(‘\033[0;33m...\ndelete success !\033[0m‘)
def update(lists,updates):
‘‘‘
数据更改操作
:param lists: 筛选出的数据id列表
:param updates: 需要更新的数据
:return:
‘‘‘
key = re.findall(‘\w+‘,updates)
with open(tables, ‘r+‘, encoding=‘utf-8‘) as f, open(‘data_new.txt‘, ‘w‘) as f2:
for line in f:
if int(line.split(‘,‘)[0]) in list(map(lambda x: x + 1, lists)):
tem = line.split(‘,‘)
tem[match[key[0]]] = key[1]
line = ‘,‘.join(tem)
f2.write(line)
os.remove(tables)
os.rename(‘data_new.txt‘, tables)
print(‘\033[0;33m%s changes ...\033[0m‘%len(lists))
def prints(projects,data_num):
‘‘‘
内容输出打印模块
:param projects: 需要打印的内容
:param data_num: 数据的id列表
:return:
‘‘‘
tem = ‘‘
tem1 = ‘‘
if ‘*‘ in projects:
projects = data.keys()
for i in projects:
tem1 += "‘%s‘.center(11,‘ ‘),‘|‘,"%i
tem += "str(data[‘%s‘][j]).center(11,‘ ‘),‘|‘," %i
eval("print(" + tem1 + ")")
for j in data_num:
print(‘- - - - - - -‘.center(14, ‘ ‘)*len(projects))
eval("print(" + tem + ")")
if __name__ == ‘__main__‘:
main()
标签:text ali hone handle item center sam 打印 手机
原文地址:https://www.cnblogs.com/q1ang/p/9404568.html