Python,属性错误:'int'对象没有属性'strip'



我想从一个简单字典结构(或类)的txt文件中提取数据,但是我得到以下错误代码:

AttributeError: 'int' object has no attribute 'strip'

完整的Python代码可以在答案中找到!

我使用的代码:

data = []
file = open("fruits.txt", "r", encoding="utf-8")
lines = file.readlines()
#I did it two ways
#Dictionary:
for i in lines:
data_line = i.strip().split(";")
data_line[2] = data_line[2].replace(",", ".") ## ! ##
dictionary = {
"fruit": data_line[0],
"piece": int(data_line[1]),
"price": float(data_line[2]) ## ! ##
}
data.append(dictionary)
file.close()
#and
#Class:
class fruits:
def __init__(self, fruit, piece, price ):
self.fruit = fruit
self.piece = int(piece)
self.price = float(price) ## ! ##
##########################################################
print(data)
##########################################################
#fruits = set()
#for i in range(len(data)):
#    fruits.add(data[i]["fruit"])
#for fruit in fruits:
#    piece = 0
#    for i in range(len(data)):
#        if data[i]["fruit"] == fruit:
#            piece += 1
#    print(f"t{fruit}: {piece} ")

# vowels list
#py_list = ['e', 'a', 'u', 'o', 'i']
#print(sorted(py_list))
# string
#py_string = 'Python'
#print(sorted(py_string))
# vowels tuple
#py_tuple = ('e', 'a', 'u', 'o', 'i')
#print(sorted(py_tuple))
#def leap_year(number):
# = int(number)
#    if (number % 4 == 0 and number % 100 != 0 or number % 400 == 0):
#        return True
#    else:
#        return False

#for i in range(1900, 2000):
#    if leap_year(i) == True:
#        print(i)

相关内容

  • 没有找到相关文章

最新更新