i am trying update a csv file (original.csv) from another csv file (first.csv) where i need to add or delete rows based action specified in first.csv column-0, i,e., add, delete.
import csv
file = 'first.csv'
with open(file) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
for row in csv_reader:
if row[0] == 'add':
with open('original.csv', 'a', newline='') as f:
writer = csv.writer(f, quotechar="'")
writer.writerow(row)
cat first.csv
add,apple,1,2,3
add,orange,1,2,3
delete,banana,1,2,3
cat original.csv
none,cherry,1,2,3
none,pineapple,1,2,3
none,banana,1,2,3
if column0 in first.csv is add, it will add that row in original file,
if column0 in first.csv is delete, it will delete that corresponding row (based on match of fields after `none`. delete,banana,1,2,3 == none,banana,1,2,3 both rows are same ignoring first field)
Output getting for above add
method..
none,cherry,1,2,3
none,pineapple,1,2,3
none,banana,1,2,3
add,apple,1,2,3
add,orange,1,2,3
please help how to develop above script, so that row corresponding to delete is removed from original file.
expected output:
none,cherry,1,2,3 # already exist in this file
none,pineapple,1,2,3 # already exist in this file
add,apple,1,2,3 # added from first.csv file
add,orange,1,2,3 #added from first.csv file.
please help.
thanks in advance.