Writing specific value back to .csv, Python
<p>I have a .<code>csv</code> file with some data that i would like to change. 
It looks like this:</p>

<pre><code>item_name,item_cost,item_priority,item_required,item_completed
item 1,11.21,2,r
item 2,411.21,3,r
item 3,40.0,1,r,c
</code></pre>

<p>My code runs most of what i need but i am unsure of how to write back on my <code>.csv</code> to produce this result</p>

<pre><code>item_name,item_cost,item_priority,item_required,item_completed
item 1,11.21,2,x
item 2,411.21,3,r
item 3,40.0,1,r,c
</code></pre>

<p><strong>My code:</strong></p>

<pre><code>print("Enter the item number:")
    line_count = 0
    marked_item = int(input())
    with open("items.csv", 'r') as f:
        reader = csv.DictReader(f, delimiter=',')
        for line in reader:
            if line["item_required"] == 'r':
                line_count += 1
                if marked_item == line_count:
                    new_list = line
                    print(new_list)
                    for key, value in new_list.items():
                        if value == "r":
                            new_list['item_required'] = "x"
                            print(new_list)
    with open("items.csv", 'a') as f:
        writer = csv.writer(f)
        writer.writerow(new_list.values())
</code></pre>

<p>I guess this should do the trick:</p>

<ol>
<li>Open a file which can read and written to update it (use "+r" for that)</li>
<li>instead of opening it again write it right there using csvfilewriter, which we create at the start.</li>
</ol>

<p>file.py</p>

<pre><code>import csv
fieldnames = ["item_name","item_cost","item_priority","item_required","item_completed"]

csvfile = open("items.csv", 'r+')
csvfilewriter = csv.DictWriter(csvfile, fieldnames=fieldnames,dialect='excel', delimiter=',')
csvfilewriter.writeheader()

print("Enter the item number:") 
line_count = 0
marked_item = int(input())
with open("items.csv", 'r') as f:
    reader = csv.DictReader(f, delimiter=',')
    for line in reader:
        if line["item_required"] == 'r':
            line_count += 1
            if marked_item == line_count:
                new_list = line
                print(new_list)
                for key, value in new_list.items():
                    if value == "r":
                        new_list['item_required'] = "x"
                        print(new_list)
                        csvfilewriter.writerow(new_list)
</code></pre>

<p>If you don't want to update the csv but want to write a new one, below is the code:</p>

<pre><code>import csv

fieldnames = ["item_name","item_cost","item_priority","item_required","item_completed"]

csvfile = open("items_new.csv", 'w')
csvfilewriter = csv.DictWriter(csvfile, fieldnames=fieldnames,dialect='excel', delimiter=',')
csvfilewriter.writeheader()

print("Enter the item number:")
line_count = 0
marked_item = int(input())
with open("items.csv", 'r') as f:
    reader = csv.DictReader(f, delimiter=',')
    for line in reader:
        if line["item_required"] == 'r':
            line_count += 1
            if marked_item == line_count:
                new_list = line
                print(new_list)
                for key, value in new_list.items():
                    if value == "r":
                        new_list['item_required'] = "x"
                        print(new_list)
                        csvfilewriter.writerow(new_list)

            else:
                csvfilewriter.writerow(line)
</code></pre>

python csv dictionary