Question: (Closed) Comparing Text Files Using Python
0
gravatar for Nitin
6.2 years ago by
Nitin130
Germany
Nitin130 wrote:

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 • 7.1k views
ADD COMMENTlink modified 6.2 years ago by Istvan Albert ♦♦ 78k • written 6.2 years ago by Nitin130

The above is not a bioinformatics question therefore will be closed. Please ask general programming questions on StackOverflow: http://stackoverflow.com/

ADD REPLYlink written 6.2 years ago by Istvan Albert ♦♦ 78k
Please log in to add an answer.
The thread is closed. No new answers may be added.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1286 users visited in the last hour