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
  Move Files based on partial Match mohamedsalih12 2 713 Sep-20-2023, 07:38 PM
Last Post: snippsat
  python print all files which contain specific word in it mg24 5 1,162 Jan-27-2023, 11:20 AM
Last Post: snippsat
  Failing to print sorted files tester_V 4 1,169 Nov-12-2022, 06:49 PM
Last Post: tester_V
  Saving the print result in a text file Calli 8 1,677 Sep-25-2022, 06:38 PM
Last Post: snippsat
  failing to print not matched lines from second file tester_V 14 5,892 Apr-05-2022, 11:56 AM
Last Post: codinglearner
  Print to a New Line when Appending File DaveG 0 1,177 Mar-30-2022, 04:14 AM
Last Post: DaveG
Sad Want to Save Print output in csv file Rasedul 5 10,598 Jan-11-2022, 07:04 PM
Last Post: snippsat
  Convert legacy print file to XLSX file davidm 1 1,754 Oct-17-2021, 05:08 AM
Last Post: davidm
  Open and read multiple text files and match words kozaizsvemira 3 6,638 Jul-07-2021, 11:27 AM
Last Post: Larz60+
  Why it does not print(file.read()) Rejaul84 1 2,298 Jul-01-2021, 10:37 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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