Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6120865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:43:02+00:00 2026-05-23T15:43:02+00:00

I have 2 tab delimited files for example: file1: 12 23 43 34 433

  • 0

I have 2 tab delimited files
for example:

file1:

12  23  43  34
433  435  76  76

file2:

123  324  53  65
12  457  54  32

I would like to loop through these 2 files, comparing every line of file1 with file2 and vice versa.
If, for example, the 1st number of 1st line in file1 is the same as the 1st number of 2nd line in file 2:
I would like to put from the 1st line in file1 in a file called output.
then I would like to put all the lines from file1 that didn’t find a match in file 2 in a new file
and all the lines from file2 that didn’t find a match in file1 in a new file.

so far I have been able to find the matching lines and put them in a file but I’m having trouble putting the lines that didn’t match into 2 separate files.

one=open(file1, 'r').readlines()
two=open(file2, 'r').readlines()
output=open('output.txt', 'w')
count=0
list1=[]    #list for lines in file1 that didn't find a match 
list2=[]    #list for lines in file2 that didn't find a match
for i in one:
    for j in two:
        columns1=i.strip().split('\t')
        num1=int(columns1[0])
        columns2=j.strip().split('\t')
        num2=int(columns2[0])
        if num1==num2:
           count+=1
           output.write(i+j)
        else:
           list1.append(i)        
           list2.append(j)

Problem I have here is with the else part.
Can someone show me the right and better way to do this, I would greatly appreciate.

EDIT: Thanks for the quick responses everyone
The 3 output I would be looking for is:

Output_file1: #Matching results between the 2 files

12 23 43 34 #line from file1
12 457 54 32 #line from file2

Output_file2: #lines from the first file that didn’t find a match

433 435 76 76

Output_file3: #lines from the second file that didn’t find a match

123 324 53 65

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:43:02+00:00Added an answer on May 23, 2026 at 3:43 pm

    Think that it is not the best way but it works for me and looks prety easy for understanding:

    # Sorry but was not able to check code below
    def get_diff(fileObj1, fileObj2):
        f1Diff = []
        f2Diff = []
        outputData = []
        # x is one row
        f1Data = set(x.strip() for x in fileObj1)
        f2Data = set(x.strip() for x in fileObj2)
        f1Column1 = set(x.split('\t')[0] for x in f1Data)
        f2Column1 = set(x.split('\t')[0] for x in f2Data)
        l1Col1Diff = f1Column1 ^ f2Column1
        l2Col1Diff = f2Column1 ^ f1Column1
        commonPart = f1Column1 & f2column1
        for line in f1Data.union(f2Data):
            lineKey = line.split('\t')[0]
            if lineKey in common:
                outputData.append(line)
            elif lineKey in l1ColDiff:
                f1Diff.append(line)
            elif lineKey in l2ColDiff:
                f2Diff.append(line)
        return outputData, f1Diff, f2Diff
    
    outputData, file1Missed, file2Missed = get_diff(open(file1, 'r'), open(file2, 'r'))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tab-delimited genome sequence files (SAM format), and I would like to
I have text files that are Tab delimited. I created a Schema.ini like so:
I have 600 tab-delimited .txt files that look like this: barcode gene.symbol value 1
I have a file in tab delimited format with trailing newline characters, e.g., 123
I have a tab navigator control and I would like to make all of
I have two tab delimited csv files (with headers) that I need to merge
I have a tab-delimited file that looks something like this: \>WB02 \t F27C8.1 IV
I have a large tab delimited text file, for example, call it john_file: 1
I have two tab delimited files: File 1: 12 rows and 1 column File
I have a php script that steps through a folder containing tab delimited files,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.