# -*- coding:utf-8 -*- #import json import shutil from prettytable import PrettyTable #print json.dumps(context, encoding="UTF-8", ensure_ascii=False) #输入姓名或手机号码,显示详细信息 c_cnt=0 #次数计数器 while c_cnt<3: #尝试超过三次即退出 #读取文件内容放入字典 with open('address_list.txt','r') as f: context={} for i in f.readlines(): line=i.strip().split() context[line[0]]=line[1:] #给字典赋值,列表的第一个值赋值为键,其余值赋为值 v_cnt=0 #数据计数器 row = PrettyTable() row.field_names = ['name','job','place','office-phone','mobile','email'] #格式化输出 v_put=raw_input("Your input is :").strip().decode('gbk').encode('utf8')#decode是解码,本例系统字符集是gbk,要转化成utf8做处理用encode(文件是utf8格式) #增删改功能 if v_put=='more' or v_put=='MORE': print '''选择功能: D:删除 I:插入 U:更新''' v_more=raw_input("Your input is :").strip() if v_more=='d' or v_more=='D' : while c_cnt<3: v_del = raw_input("please input the info to delete: ").strip().decode('gbk').encode('utf8') if len(v_del) == 0: print "empty input,try again!" c_cnt+=1 elif v_del=='exit' or v_del=='EXIT': break else: # 输入值与源文件比对,有则丢弃,没有则添加到新文件,最后新文件覆盖源文件,实现删除功能 v_dcn=0 with open('address_list.txt','r') as yy: with open('address_list_bak.txt','a') as bb: with file('address_list_new.txt','a') as nn: for line in yy.readlines(): if v_del in line: bb.write(line) v_dcn+=1 print "\033[32;1mdelete\033[0m"+": "+line elif v_del not in line: nn.write(line) if v_dcn==0: c_cnt+=1 print "The data is not exsist!" elif v_dcn>0: break shutil.move('address_list_new.txt','address_list.txt') elif v_more=='i' or v_more=='I' : while v_cnt<4: v_ins1 = raw_input("please input name: ").strip().decode('gbk').encode('utf8') v_ins2 = raw_input("please input job: ").strip().decode('gbk').encode('utf8') v_ins3 = raw_input("please input place: ").strip().decode('gbk').encode('utf8') v_ins4 = raw_input("please input office-phone: ").strip().decode('gbk').encode('utf8') v_ins5 = raw_input("please input mobile: ").strip().decode('gbk').encode('utf8') v_ins6 = raw_input("please input email: ").strip().decode('gbk').encode('utf8') if len(v_ins1) == 0 or v_ins5 == 0: print "Name or mobile is empty ,try again!" c_cnt+=1 continue else: with open('D:\python_pro\\address_list.txt','a') as yy: yy.write('\n'+v_ins1+' '+v_ins2+' '+v_ins3+' '+v_ins4+' '+v_ins5+' '+v_ins6) break elif v_put=='exit' or v_put=='EXIT': break else: if len(v_put)==0 or (v_put[0]=='1' and len(v_put)<4) : #限制输入的数字,否则会泄露数据,列入输入1 ,则返回所有手机号 c_cnt+=1 print "Your input is not valid!" continue elif v_put[0]=='1' and len(v_put)>=4 : for num,con in context.items(): #遍历所有信息,有则计数器加一,并输出 if con[3].count(v_put)!=0: #count是列表的属性,返回element在列表某个值中出现的次数,通过次数来做模糊匹配 v_cnt+=1 row.add_row(num.split()+con) #num.split() 字符串+".split()",可以对字符串切片,并转换为列表 print row #使用json处理字典内部汉字乱码问题 print json.dumps(num, encoding="UTF-8", ensure_ascii=False),json.dumps(con, encoding="UTF-8", ensure_ascii=False) if v_cnt>0: print 'Found %s items.' % v_cnt else: c_cnt+=1 print "The number is not found !" else : for name in context.keys(): #遍历所有key,有则计数器加一,并输出 if name.count(v_put)>0: #字典的模糊匹配 v_cnt+=1 row.add_row(name.split()+context[name]) if v_cnt>0: print row print 'Found %s items.' % v_cnt else: c_cnt+=1 print "The name is not found !"