gpt4 book ai didi

python - 代码未读取文件,错误对象 Len() MASTERLIST

转载 作者:太空宇宙 更新时间:2023-11-03 21:29:38 25 4
gpt4 key购买 nike

这是我第一次在这里发帖。我正在做一项作业,但我遇到了困难。

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()

最佳答案

仅仅打开文件是不够的,您还需要从中.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 - Reading and Writing Files

关于python - 代码未读取文件,错误对象 Len() MASTERLIST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53607167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com