Wait for bloom
时光不语,静待花开

实现文件增删改查操作                

1.写入文件

'''以w形式打开文件,写入记录'''

1 #!/usr/bin/python
2 with open('test','w',encoding='utf-8') as f:
3     '''插入数据'''
4     f.writelines('1,Alex Li,22,13651054608,IT,2013-04-01\n')
5     f.writelines('2,chao zi,30,17886999022,HR,2015-05-03\n')
6     f.writelines('3,rain li,25,13832353221,Sales,2016-04-22\n')
7     f.writelines('4,mack ci,40,13456789876,HR,2009-03-01\n')
8     f.flush()
View Code

2.根据需求查询内容

  • 根据年龄查询比输入年龄大的内容
  • 查询输入部门的记录
  • 根据年份查询记录
  • 显示查询的记录条数
 1 def search_text():
 2    '''#select name,age from staff_table where age > 22'''
 3    global age, dept, year
 4    choice= input('请选择进入哪个查询:年龄,部门,年份')
 5    if choice =='年龄':
 6         age =int(input('请输入年龄:'))
 7         search_age()
 8    elif choice=='部门':
 9        dept =input('请输入部门')
10        search_dept()
11    elif choice=='年份':
12         year =input('请输入年份')
13         search_year()
14    else:
15         print('请输入正确的值')
16 def search_age():
17     '''select  * from staff_table where dept = "IT"'''
18     with open('test','r+',encoding='utf-8') as f:
19         n=0
20         for i in f:
21             x=i.split(',')
22             if int(x[2]) >age:
23                 print('大于%s岁的是%s年龄是%s'%(age,x[1],x[2]))
24                 n+=1
25         print('共找到%s条数据'%n)
26 
27 def search_dept():
28     '''#select  * from staff_table where dept = "IT"'''
29     with open('test','r+',encoding='utf-8') as f:
30         n=0
31         for i in f:
32             x=i.split(',')
33             if x[4] ==dept:
34                 print('在%s部门的是'%dept,i)
35                 n+=1
36         print('共找到%s条数据' % n)
37 
38 
39 def search_year():
40     '''#select  * from staff_table where enroll_date like "2013"'''
41     with open('test','r+',encoding='utf-8') as f:
42         n=0
43         for i in f:
44             x=i.split(',')
45             # print(x[5].startswith('2013'))
46             if x[5].startswith(year)==True:
47                 print(i)
48                 n+=1
49         print('共找到%s条数据' % n)
50 
51 
52 search_text()
View Code

3.新增记录,要求phone唯一,并且id自增

  • phone唯一:要求比较文件里面的phone,判断是否存在,不存在则新增
  • id自增:取文件里面id最大的值,在基础上加1
 1 def insert_line(name,age,phone,dept,date):
 2     '''可创建新员工纪录,以phone做唯一键,staff_id需自增wi'''
 3     status =True
 4     with open('test','r+',encoding='utf-8') as f:
 5         id=[]
 6         for i in f:
 7             x=i.split(',')
 8             if x[4] ==phone:
 9                 status=False
10             id.append(x[0])
11         if status==True:
12          f.writelines('%s,%s,%s,%s,%s,%s'%(int(max(id))+1,name,age,phone,dept,date))
13 insert_line('zhang yun','22','17002991187','IT','2024-01-25')
View Code

 

4.根据id删除记录

  • 删除记录:读取原来的文件到新文件,如果是输入id则不写入到新文件
 1 def del_line(id):
 2     '''输入id删除该id的记录'''
 3     with open('test','r+',encoding='utf-8') as f ,\
 4         open('test1', 'w+', encoding='utf-8') as f1:
 5         for i in f:
 6             x=i.split(',')
 7             if id ==int(x[0]):
 8                 continue
 9             else :
10                 f1.writelines(i)
11 
12 del_line(2)
View Code

5.修改信息

  • 同删除记录(同样是修改文件):读取原来的文件到新文件,如果遇到需要修改的内容,则替换为新内容再写入到新文件
 1 def update_line(dept_old,dept_new):
 2     '''UPDATE staff_table SET dept="Market" WHERE where dept = "IT"'''
 3     with open('test','r+',encoding='utf-8')as f,open('test2','w+',encoding='utf-8')as f1:
 4         for i in f:
 5             x=i.split(',')
 6             if x[4]==dept_old:
 7                 i=i.replace(dept_old,dept_new)
 8             f1.writelines(i)
 9 
10 
11 
12 update_line('IT','Market')
View Code

 

posted on 2024-01-25 14:37  Little-Girl  阅读(82)  评论(0)    收藏  举报