Off topic:Comparing Text Files Using Python
0
0
Entering edit mode
11.6 years ago
Nitin ▴ 170

Hi all

I have 29 text files as follows

File 1

Comp1 : 23546

    12313
    23214
    32465

File 2

Comp2 : 23546

    12323
    32125
    32125
    32121

I would like to have python script which computes intersection between these files and finally want to print first column ID and common id between files as follows

Comp1 : 23546
Comp2 : 23546

I have tried following it doesnt work

file1=open("com1.txt","r")
file2=open("com3.txt","r")
file3=open("Overlap_compound1_2","w")
list1=file1.readlines()
list2=file2.readlines()

for line1 in list1:
  for line2 in list2:
    if line1.strip() in line2.strip().split(' '):
      print line2
      file3.write(line2)

Thanks

Ni

python • 8.8k views
ADD COMMENT
This thread is not open. No new answers may be added
Traffic: 3000 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6