这是我第一次在这里发布。我正在做我的一项任务,现在很辛苦。
Here is what my professor is asking for - click to see the screenshot
这是我的代码:
option1=open("BrandVB.txt", "r")
option2=open("BrandA.txt", "r")
option3=open("BrandC.txt", "r")
option4=open("BrandX.txt", "r")
#Creating a master lits
li = [option1, option2,option3, option4]
#Searching part numbers based on brand
brand=input("Enter the brand: ")
for i in range(len(li)):
for j in range(len(li[i])):
if brand==li[i][j]:
pos=j
for i in range(len(li)):
print(li[i][pos])
我得到的错误是:
Traceback (most recent call last):
File "/Users/admin/Desktop/Desktop/LAB10/LAB10.py", line 17, in <module>
for j in range(len(li[i])):
TypeError: object of type '_io.TextIOWrapper' has no len()
答案 0 :(得分:0)
您需要实际阅读课文。现在,您只需将文件作为IO包装器打开,而无需使用它们来输入数据。
尝试一下:
with open("BrandVB.txt", "r") as file:
option1 = file.read()
答案 1 :(得分:0)
仅open
个文件还不够,您还需要.read()
。这是一种实现方法:
with open("BrandVB.txt", "r") as a, open("BrandA.txt", "r") as b, open("BrandC.txt", "r") as c, open("BrandX.txt", "r") as d:
option1 = a.read()
option2 = b.read()
option3 = c.read()
option4 = d.read()