Python Forum
Look for match in two files and print out in the first file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Look for match in two files and print out in the first file
#1
Hi,

I have two files and if Original.csv match in the 3 column it should then add the text Hello in the first file.
If a value in Original.csv does not exists in Random.csv it should not add Hello

reader1 = csv.reader(open('Original.csv', 'r'), delimiter=';')
row1 = next(reader1)
reader2 = csv.reader(open('Random.csv', 'r'), delimiter=';')
row2 = next(reader2)
if (row1[3] == row2[3]):
   print("Hello", row1)


Orginal.csv contains:

Quote:20200302075825;XAREAXAREAXAREAXARE5AXARE5AXA3REAXA;Alpha;46789877887;Direct;5;0;1113
20200302075825;XAREAXAREAXAREAXAREAXAREAX2AREAXA;Alpha;4644444444444;Direct;5;0;1113
20200302075825;1709A40CEC90001ABDD136E56F4983BF;Alpha;46700000922222;Direct;5;0;1113
20200302075825;1709A40CEC90001ABDD136E56F4983BF;Alpha;46700000333333;Direct;5;0;1113

Random.csv contains:
20200302075825;XAREAXAREAXAREAXARE5AXARE5AXA3REAXA;Test;46789877887;Direct;5;0;1113
20200302075825;XAREAXAREAXAREAXAREAXAREAX2AREAXA;Test;4644444444444;Direct;5;0;1113
20200302075825;1709A40CEC90001ABDD136E56F4983BF;Test;46700000922222;Direct;5;0;1113

So the output should be in Original.csv

20200302075825;XAREAXAREAXAREAXARE5AXARE5AXA3REAXA;Alpha;46789877887;Direct;5;0;1113;Yes
20200302075825;XAREAXAREAXAREAXAREAXAREAX2AREAXA;Alpha;4644444444444;Direct;5;0;1113;Yes
20200302075825;1709A40CEC90001ABDD136E56F4983BF;Alpha;46700000922222;Direct;5;0;1113;Yes
20200302075825;1709A40CEC90001ABDD136E56F4983BF;Alpha;46700000333333;Direct;5;0;1113
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  get two characters, count and print from a .txt file Pleiades 9 529 Oct-05-2020, 09:22 AM
Last Post: perfringo
  Cannot redirect print to a file tester_V 3 364 Sep-11-2020, 12:21 AM
Last Post: tester_V
  Need print out lines before and after the match tester_V 6 553 Aug-11-2020, 02:50 AM
Last Post: deanhystad
  print(elem) to .txt files kristianpython 1 306 May-15-2020, 10:55 AM
Last Post: scidam
  Print/write to file function tpolim008 4 621 Apr-01-2020, 07:59 PM
Last Post: tpolim008
  Print date, Time and output to file tpolim008 3 537 Mar-26-2020, 06:49 PM
Last Post: ndc85430
  Details of attachment files in a msg file such as file names save into a python list klllmmm 2 1,621 Nov-12-2019, 05:59 AM
Last Post: klllmmm
  How do you replace a word after a match from a list of words in each line of a file? vijju56 1 1,053 Oct-17-2019, 03:04 PM
Last Post: baquerik
  How to match two CSV files timlamont 9 1,268 Oct-01-2019, 05:54 PM
Last Post: timlamont
  Open and read multiple text files and match words kozaizsvemira 2 2,499 Sep-11-2019, 12:58 PM
Last Post: kozaizsvemira

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020