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>There are several problems here</p>

<ul>
<li>you're using a <code>DictReader</code>, which is good to read data, but not as good to read and write data as the original file, since dictionaries do not ensure column order (unless you don't care, but most of the time people don't want columns to be swapped). I just read the title, find the index of the column title, and use this index in the rest of the code (no dicts = faster)</li>
<li>when you write you append to the csv. You have to delete old contents, not append. And use <code>newline=''</code> or you get a lot of blank lines (python 3) or <code>"wb"</code> (python 2)</li>
<li>when you read, you need to store all values, not only the one you want to change, or you won't be able to write back all the data (since you're replacing the original file)</li>
<li>when you modify, you do overcomplex stuff I just replaced by a simple replace in list at the given index (after all you want to change <code>r</code> to <code>x</code> at a given row)</li>
</ul>

<p>Here's the fixed code taking all aforementioned remarks into account</p>

<p>EDIT: added the feature you request after: add a <code>c</code> after <code>x</code> if not already there, extending the row if needed</p>

<pre><code>import csv

line_count = 0
marked_item = int(input())
with open("items.csv", 'r') as f:
    reader = csv.reader(f, delimiter=',')
    title = next(reader)  # title
    idx = title.index("item_required")  # index of the column we target

    lines=[]
    for line in reader:
        if line[idx] == 'r':
            line_count += 1
            if marked_item == line_count:
                line[idx] = 'x'
                # add 'c' after x (or replace if column exists)
                if len(line)&gt;idx+1:  # check len
                   line[idx+1] = 'c'
                else:
                   line.append('c')
        lines.append(line)

with open("items.csv", 'w',newline='') as f:
    writer = csv.writer(f,delimiter=',')
    writer.writerow(title)
    writer.writerows(lines)
</code></pre>

python csv dictionary