【发布时间】:2021-01-01 12:46:18
【问题描述】:
我正在尝试编写此代码,以便用户被询问要搜索哪一行,然后他们输入它,然后您显示用户想要的行。
import os
f = open("input_file_lab_1.txt","r")
allData = f.read()#3
cleanedData = allData.split("\n")
name = input("enter line to search\n").strip()
n = 0
while n == 0:
for line in range(len(cleanedData)):
if name in cleanedData[line]:
print(name)
n = 1
else:
pass
f.close()
不确定是否有更简单的方法。我也不断收到错误
File "os1.py", line 7, in <module>
name = input("enter line to search\n").strip()#5
AttributeError: 'int' object has no attribute 'strip'
【问题讨论】:
-
你确定你使用的是 Python 3 吗?
-
我刚刚看到我使用的第一个错误我没有将它作为 python3 运行,但它仍然不起作用
-
您在使用 Python 3 时仍然遇到同样的错误吗?
-
看上面,代码没有正确缩进。请发布您正在运行的确切代码。
-
不,它让我输入用户输入,我尝试使用数字 1,然后它只是输出了一堆 1
标签: python python-3.x file input kali-linux