def write_to_csv():
newRow = ['P12467','Cat','Ancora','Yes','Yes','D123456','Lost','14/09/2019','30/09/2019','return to owner',"290 Oak Avenue, BS79 8TR"]
//with open('DADSA 2019-20 CWK A DATA PETS.csv', 'wb') as csv_file:
with open("DADSA 2019-20 CWK A DATA PETS.csv", mode="a", newline='') as csv_file:
# reader = csv.reader(csv_file)
writer = csv.writer(csv_file)
for row in csv.reader(csv_file):
if row[0] == newRow[0]:
writer.writerow(newRow)
我在csv.reader(csv_file)中的行中收到此错误: io.UnsupportedOperation:不可读”我真的不明白它的意思。
答案 0 :(得分:1)
您有错字,应该使用newRow而不是csvFile,下面是固定代码:
def write_to_csv():
newRow = ['P12467','Cat','Ancora','Yes','Yes','D123456','Lost','14/09/2019','30/09/2019','return to owner',"290 Oak Avenue, BS79 8TR"]
#with open('DADSA 2019-20 CWK A DATA PETS.csv', 'wb') as csv_file:
with open("DADSA 2019-20 CWK A DATA PETS.csv", mode="a", newline='') as csv_file:
# reader = csv.reader(csv_file)
writer = csv.writer(csv_file)
for row in csv.reader(newRow):
if row[0] == newRow[0]:
writer.writerow(newRow)
答案 1 :(得分:0)
您无法读取以a
模式打开的文件。这是用于附加到文件。您可以使用a+
模式,但需要先搜索到文件的开头。
但是,如果您只想添加新行,则无需先读取文件。只需打开文件并写入新行即可。
def write_to_csv():
newRow = ['P12467','Cat','Ancora','Yes','Yes','D123456','Lost','14/09/2019','30/09/2019','return to owner',"290 Oak Avenue, BS79 8TR"]
with open("DADSA 2019-20 CWK A DATA PETS.csv", mode="a", newline='') as csv_file:
writer = csv.writer(csv_file)
writer.writerow(newRow)