0

my problem is that I have a text file:

['food', 5, 'wood', 0, 'stone', 0, 'iron', 0, 'gold', 0, 'diamond', 0, 'wood_sword', 0, 'iron_sword', 0, 'diamond_sword', 0]

and I want to convert it directly into a list after changing one of the numbers:

def getinv(): #reads inv.txt and creates an alternating array of str, int, str, int...
    sfile = open("inv.txt", "r")
    listline = sfile.readlines()
    for i in range(len(listline)):
        linepart = listline[i]
        listline[i] = linepart.strip('\n')
        if i % 2 == 1: #every second line (listine[1,3,5...]) is the amount
            listline[i] = int(linepart.strip('\n'))
    print(listline)
    sfile.close()
    return listline

inv = getinv()
item = str(input("enter item: "))
amt = int(input("Enter the amount"))

for i in range(len(inv)):
    if i % 2 == 0 and inv[i] == item:
        namenum = i + 1 #gets the right index of 'inv' list

ifile = open("inv.txt", "w")
ogamt = inv[namenum]
totalamt = ogamt + amt
inv[namenum] = totalamt
for i in range(len):
    #enter suggestions

thank you in advance for your help

  • Welcome to Stack Overflow. Rather than doing it this way, consider using a standard file format such as JSON to represent the data. – Karl Knechtel Sep 18 '22 at 14:42

0 Answers0