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>Using <code>pandas</code>:</p>

<pre><code>import pandas as pd

df = pd.read_csv("items.csv")

print("Enter the item number:")
marked_item = int(input())

df.set_value(marked_item - 1, 'item_required', 'x')

# This is the extra feature you required:
df.set_value(marked_item - 1, 'item_completed', 'c')

df.to_csv("items.csv", index = False)
</code></pre>

<p>Result when marked_item = 1:</p>

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

<p>Note that according to <a href="https://tools.ietf.org/html/rfc4180" rel="nofollow">RFC4180</a> you should keep the trailing commas.</p>

python csv dictionary