python-3.xcsv-write-stream

Python writing in a csv-file: io.UnsupportedOperation: not writable


I am trying to read a complete csv file, alter it at one point and write it back.

This is my Code:

def change_Content(AttributIndex: int, content: str, title: str):

with open("Path.csv") as csvfile:
    csv_reader = csv.reader(csvfile)
    counter = 0
    liste=[]
    for row in csv_reader:
        
        liste.append(list(row))

        if row[0].__eq__(title):
            list[counter][AttributIndex] = content
        counter += 1

    
    csv_writer = csv.writer(csvfile)
    for row in liste:
        csv_writer.writerow(row)   # io.UnsupportedOperation: not writable

Solution

  • You are missing the mode when you open the file. By default that goes to read mode. (r).

    You need to either open with r+ or with a for appending in the end of it but if you reopen it later.

    Either

    with open("Path.csv", "r+") as csvfile:
    

    or if you reopen later

    with open("Path.csv", "a") as csvfile:
    

    should do it.