Hi,
I need to append some of the rows in a .csv file from another file. I have never done it before, I found some snippets online to do that.
I thought I should do it line by line.
First, I wanted to find lines for appending in a File1, then find the same in a File2 and use the last element of File2 to append to the specified lines in file1.
I stuck on the “find lines in File1/2. I need to match:
row[0] and row[1] File1 to :
row[0] and row[1] File2
Rows in File1 look like this:
match1, match2,something else,
Rows in File2 looking like this:
match1,match2,need this part for appending to File1,
I need to append some of the rows in a .csv file from another file. I have never done it before, I found some snippets online to do that.
I thought I should do it line by line.
First, I wanted to find lines for appending in a File1, then find the same in a File2 and use the last element of File2 to append to the specified lines in file1.
I stuck on the “find lines in File1/2. I need to match:
row[0] and row[1] File1 to :
row[0] and row[1] File2
Rows in File1 look like this:
match1, match2,something else,
Rows in File2 looking like this:
match1,match2,need this part for appending to File1,
Python:
from csv import writer
from csv import reader
match1 = 'ABBV50001'
match2 = 'CELL'
with open('C:/scripts/CSV/1.csv', 'r') as read_f1, open('C:/scripts/CSV/2Ping.csv', 'r') as read_f2 :
csv_reader1 = reader(read_f1)
csv_reader2 = reader(read_f2)
for row in csv_reader1 :
if row[0] == match1 and row[1] == match2 :
print (row)
for row in csv_reader1 :
if row[0] == match1 and row[1] == match2 :
print (row)