这是我第一次在这里发帖。我正在做我的一项任务,我过得很艰难。
这是我的教授所要求的-点击查看截图
这是我的代码:
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()
仅仅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()
Python-读取和写入文件
您需要实际阅读文本。现在,您只需将文件作为IO包装器打开,而不需要使用这些文件来输入数据。
试试这个:
with open("BrandVB.txt", "r") as file:
option1 = file.read()