Skip to main content
 首页 » 编程设计

python之读取文本文件时字符串索引超出范围

2025年04月02日24qq78292959

<分区>

我一直在我的程序的倒数第二行出现这个错误,我不确定为什么,我所做的只是从文本文件中读取一行。

if (items[0]) == 86947367 : 
        with open("read_it.txt") as text_file: 
            try: 
                price = int(text_file.readlines()[2]) 
            except ValueError: 
                print("error") 
            else: 
                new_price = int(price * (items2[0])) 
                print("£",new_price) 
                price_list.append(new_price) 
                product = (text_file.readline()[1]) 
                print(product)