【发布时间】:2017-05-03 10:49:59
【问题描述】:
我正在尝试从用户那里获取输入并从文件中搜索字符串,然后打印该行。当我尝试执行时,我不断收到此错误。我的代码是
file = open("file.txt", 'r')
data = file.read()
zinput = str(input("Enter the word you want me to search: "))
for zinput in data:
line = data.readline()
print (line)
【问题讨论】:
-
你的代码在很多层面都是错误的。您从文件中执行了
read(),然后在循环中使用readline(),这基本上会覆盖用户输入。 -
然后用数据中的每一行覆盖输入调用中的
zinput。