重写csv某行
import csv
to_path="1.csv"
to_path_="1的备份最终用这个文件.csv"
with open(to_path, 'r') as f1:
read_it = csv.reader(f1)
show_rows = [row for row in read_it]
for i in show_rows:
if i[0]=="ttt": # 如果第一列 的值等于 "ttt" 就重写
os.remove(to_path_) #删掉预留的重写文件
re_write=[i[0], i[1],i[2],i[3],i[4],"8881"] #标记要重写的内容
with open(to_path, 'r', newline='') as f_old, \
open(to_path_, 'w', newline='') as f_new:
f_csv_old = csv.reader(f_old)
f_csv_new = csv.writer(f_new)
for iw, rows in enumerate(f_csv_old): # 重写header
if iw == 0:
f_csv_new.writerow(rows)
break
for rows in f_csv_old:
if rows[0] != "ttt": # 第一列值为ttt的行 不进行重写
f_csv_new.writerow(rows)
f_csv_new.writerow(re_write) # 重写标记的行
else:
pass
===今日我言===========
勇气 让人进步
===================
浙公网安备 33010602011771号