I know that it is not possible to write to a specific cell in a csv file , but in my code I have been trying to change a value if a command is met. So if the user enters a number higher than the target stock ( the suitable stock amount , last column) then it will subtract the entered number from the current stock ( 3rd column) and then write back the new stock.The problem with my code that it writes over everything :
before writing to csv :
86947367,banana,100,40
78364721,apple,50,20
35619833,orange,20,30
84716491,sweets,200,90
46389121,chicken,5,10
after writing to csv:
86947367,banana,2,40
As you can see it overwrites the whole stock file with the row that the stock has changed in.
my current code :
import csv
file=open("stock.csv")
stockfile= csv.reader(file)
for line in stockfile:
    if GTIN in line:
        currentstock= line[2]
        targetstock = line[3]
        newstock = (int(currentstock) - int(Quantity))
        targetstock = str(targetstock)
        newstock = str(newstock)
        if newstock < targetstock :
            with open('stock.csv', 'r') as infile:
                oldata = csv.reader(infile)
                for row in oldata:
                    if GTIN == row[0]:
                        newstock=int(row[2]) - int(Quantity)
                        row[2] = newstock
                        print(row)
                        with open("output.csv","w") as outfile:
                            newdata = csv.writer(outfile)
                            newdata.writerow(row)
        else:
            print("lel")
All help appreciated.