Есть несколько способов сделать это.
Но следуя тому, как вы уже это делали, вы можете просто включить количество при чтении файла. Код будет выглядеть примерно так:
def feature_4(flower_file='flowers.txt'):
flower_update = input("Enter the name of the flower you wish to change the price:"
"Lily, Rose, Tulip, Iris, Daisy, Orchid, Dahlia, Peony")
flower_new_price = input("Enter the updated price of the flower")
flower, price, quantity = [], [], []
with open(flower_file) as amend_price:
for line in amend_price:
spt = line.strip().split(",")
flower_price = int(spt[1])
flower_name = str(spt[0])
quantity.append(str(spt[2]))
if flower_name == flower_update :
price.append(flower_new_price)
else:
price.append(flower_price)
flower.append(flower_name)
with open(flower_file, "w") as f_:
for i, v in enumerate(flower):
f_.write("{},{},{}\n".format(v, str(price[i]),quantity[i]))
print("The new price of", flower_update, "is", flower_new_price)
В качестве альтернативы, если вы хотите обновить и не перезаписать весь файл, вам нужно открыть файл с помощью open('txtfile.txt','a+')
. и перейдите к указанной строке, которую вы хотите добавить.