import codecs """ file = codecs.open(r"路径","rb","gbk","ignore") for line in file: # 硬盘模式 print(line) file.close() """ def loadata(): global datalist # 引用全局变量 把加载的内容放到全局变量中 file = codecs.open(r"路径", "rb", "gbk", "ignore") datalist = file.readlines() file.close() # 保存变量 def search(namestr):
# 要保存文件名字和路径 savefilepath = "路径" + namestr +".txt" savefile = open(savefilepath,"wb") numbers = 0 for line in datalist: if line.find(namestr) != -1: print(line,end="") # 显示数据 numbers += 1 savefile.write(line.encode("utf-8")) # 写入数据 savefile.write(("数量" + str(numbers)).encode("utf-8")) savefile.close() datalist = [] print("加载开始") loadata() print("加载结束") while True: # 查询人的名字 searchname = input("要查询的数据") search(searchname)