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

  • SEARCH
  • Home
  • 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 8084155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:42:10+00:00 2026-06-05T17:42:10+00:00

i have been trying to write a program in python to read two text

  • 0

i have been trying to write a program in python to read two text files and print the differences in their texts.the two files are similar other than the line numberings which are different owing to some comments that have been inserted.I have tried using difflib module but it is giving me errors.

import difflib
from difflib import *

temp3=[]
temp4=[]
with open ("seqdetect",'r') as f:
    with open ("seqdetect_2",'r') as g:
        for item in f:
            temp1 =item.split()
            temp3.append(temp1)

        for items in g:
            temp2 =items.split()
            temp4.append(temp2)
        d = difflib.Differ()
        diff = d.compare(temp3, temp4)
        print ('\n'.join(diff))

Could you please suggest an alternative.
Regards,
Mayank

  • 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-06-05T17:42:12+00:00Added an answer on June 5, 2026 at 5:42 pm

    Ok, I’ve tried out your code and found the issue.

    The Differ.compare() method expects to be given two lists of strings, representing the lines of your two texts. However, because of your item.split() calls, your lists temp3 and temp4 are lists of lists of (one character long) strings.

    I’m not sure exactly what you were wanting to do with that split, so I’m not sure what the best fix is. If you really do want it to tell you the individual characters that have been added or removed, you can replace your calls to append() with extend() in the two for loops. But that doesn’t seem very useful, frankly.

    More likely the splitting is a mistake. Rather than looping over the lines in your files, just read them all into lists using readlines() and let the Differ do its work on them.

    with open("seqdetect") as f, open("seqdetect_2") as g:
        flines = f.readlines()
        glines = g.readlines()
    
        d = difflib.Differ()
        diff = d.compare(flines, glines)
        print("\n".join(diff))
    

    If you want to do some filtering on what counts as a difference (ignoring whitespace differences, or whatever) you should explore the difflib documentation, and pass an appropriate function as the linejunk or charjunk parameters of the Differ’s constructor.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to write a program to read columns of text-formatted numbers into
I am trying to re-write a dictionary program in python(3.X). I have been using
Hii , I have been trying to write a program... We have a structure
I have been trying to write a powershell program that checks to see if
I have been trying to write a program to convert my father's old PTS.
I have been trying to write a C program which generates all possible permutations
I have been trying to figure out how to write a simple program to
I have been trying to write a program for transferring a file which the
I have been trying to write a bare-bones ping scanner using Perl for internal
I have been trying to write an image on a layer using Quartz but

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.