csv, python, update line
Question: I am looking to update specific lines of a csv as I run through them in a for
loop. For example:

line_of_csv = "item1,item2"

for row in csv: if action: #line of code to write "action occurred" output:
(for lines that action occurred: "item1,item2,action occured") (for lines that
action didn't occur: "item1,item2"

ideally this would remain in the same file and not have to write a second
file. I hope this makes sense and any input is greatly apprecaited!


Answer: my.csv

    
    
    col1,col2
    item1,item2
    item3,item4
    item5,item6
    item7,item8
    item9,item10
    

code

    
    
    import csv
    
    result = []
    with open('my.csv', 'rb') as in_file:
        reader = csv.reader(in_file)
        for line_num, line in enumerate(reader):
            if line_num == 1:
                line = ['modified', 'line']
            result.append(line)
    
    with open('my.csv', 'wb') as out_file:
        writer = csv.writer(out_file)
        writer.writerows(result)
    

