问题描述
我正在尝试读取一个完整的 csv 文件,在某一时刻对其进行更改并将其写回。
这是我的代码:
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
解决方法
尝试使用 open("Path.csv",mode="r+") 打开具有读写权限的文件。
,您在打开文件时缺少模式。 默认情况下进入阅读模式。 (r)。
您需要使用 r+ 打开或在其末尾附加一个 for ,但如果您稍后重新打开它。
要么
with open("Path.csv","r+") as csvfile:
或者如果您稍后重新打开
with open("Path.csv","a") as csvfile:
应该这样做。