0
quantity
を.txtファイルの一致crop
と同じ行に追加したいと考えています。しかし、現時点では私はエラー:ValueError: '(whichever string is contained within crop)' is not in list'
を取得します。これは、crop
が各行の最初の要素と比較されるように、各行の要素を分割する必要があるためです。この変数を.txtファイルの特定の変数と同じ行に追加するにはどうすればよいですか?
crop = input("Which crop? ")
quantity = input("How many? ")
with open ('cropdatabase.txt', 'a+') as file:
lines = file.readlines
index = lines.index(crop)
lines[index] += ' ' + str(quantity)
file.close()
The .txt file is formatted like this