|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
初学python在做文件内容查找时使用for遍历文件内容,最后打印出来的结果有一个小尾巴。- import sys
- sys.path.append('C:/Users/sunshineyilan/Desktop/study')
- file1 = open('demo_file.txt','w')
- file1.writelines(' num:1 sex:male phone:1111 \n num:2 sex:female phone:2222 \n num:3 sex=male phone:3333 \n')
- file1.close()
- file1 = open('demo_file.txt','r')
- readfile = file1.readlines()
- while True:
- guest_input = input('enter the keywords you want to search for:').strip()
- if len(guest_input) == 0:
- print('please enter the keywords what you want to search:')
- continue
- else:
- for line in readfile:
- if guest_input in line:
- print(line)
- file1.close()
- else:
- print("have no data.")
复制代码 附件中有一个错误的截图。
最后想做到的样子:输入1 或者2 3的时候不会显示have no data,输入num的时候会显示所有带有num字符的行 |
|