how to .writerow() to only 1 row in csv file ?
<p>guys currently in my code it changes the 3rd row but for all rows , I want it to only change the row with the entered GTIN by the user.</p>

<p>current code :</p>

<pre><code>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))
        currentstock = str(currentstock)
        targetstock = str(targetstock)
        newstock = str(newstock)
    if newstock &lt; targetstock :
        import csv
        reader = csv.reader(open('stock.csv',"r"))
        new = csv.writer(open('out.csv',"w"))
        for line in reader:
            new.writerow([line[0], line[1], newstock , line[3]])
</code></pre>

<p>output in file ( it changes all numbers in 3rd column):</p>

<pre><code>86947367,banana,1,40
78364721,apple,1,20
35619833,orange,1,30
84716491,sweets,1,90
46389121,chicken,1,10
</code></pre>

<p>How can I only chnage the row with the GTIN the user enters ?</p>

<p>use the <code>csv</code> module:
<a href="https://docs.python.org/3/library/csv.html" rel="nofollow">https://docs.python.org/3/library/csv.html</a></p>

<p>It has a <code>csv.reader()</code> and <code>csv.writer()</code>.  Read the file into memory, iterate over it doing calcs/replacements, then write each row to a new list.  Finally, generate a new data file to replace the old one.</p>

python file python-3.x csv