搜索用户输入的txt文件。
Python你好,这是我的第一次尝试,我想实现的是一个脚本,该脚本将要求用户输入,搜索一个txt文件并显示输出或给未找到信息。理想我想问用户一旦第一个输出为进一步实现用户输入如果需要,否则退出程序。然而,如果实现了第一个目标会很高兴。下面是我可以,问题是它是打破第一次发现后,下落我知道有额外的txt文件中的匹配。我想我可能需要更换,如果一段时间,让我尝试错误,我无法工作。
选择 | 换行 | 行号
- print"\nSEARCHING NUMBERS"
- print "\nPlease Enter the Number 1, 2, 3, 4 or 5."
- text_file = open("read_it.txt", "r")
- word = raw_input("Type the Number you want to check: ") word = word.upper()
- print "\nnumber."
- for lines in text_file:
- # if the line does not contain the typed number
- # then continue to the next line
- if ''.join(lines).find(word) == -1:continue
- print lines
- break
- else:
- print "Not found" # Executed whenever break above is NOT executed
- print raw_input("\n\nPress the enter key to exit.")
- text_file.close()
我只是一个初学者,所以将不胜感激任何帮助或建议谢谢。L:)