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 9251183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:37:58+00:00 2026-06-18T10:37:58+00:00

I am a python noob, and I am attempting to compare values between lines

  • 0

I am a python noob, and I am attempting to compare values between lines in two files and output the “line name” followed by a 1 if the line is in the second file and a 0 if the line is missing from the second file. The first iteration returns a 1, because that line is in the second file, but for the remaning > 1,000 lines, they all return a 0 regardless of whether they are in the second list or not. It seems as though the second “for loop” only executes on the first iteration. Any ideas on why? Here is my code:

    import sys  

    file1 = sys.argv[1] 

    file2 = sys.argv[2]

    name = str(file2)

    f1 = open(file1, 'r') 
    f2 = open(file1, 'r')
    o1 = open((name + '1.txt'), 'w')

    for line in f1:
        name = line.strip('\r\n')
        count = 0
        for line1 in f2:
            if name == line1.strip('\r\n'):
                count += 1
                print (str(name) + '\t' + str(1))
                o1.write(str(name) + '\t' + str(1) + '\r\n')
        if count == 0:
            print (str(name) + '\t' + str(0))
            o1.write(str(name) + '\t' + str(0) + '\r\n')

    f1.close()
    f2.close()
    o1.close()
Any help is very much appreciated!

After some changes, this is what I have and it only returns ‘1s’

f1 = open(file1, 'r') #opens files for reading
f2 = open(file2, 'r')
o1 = open((name + '1.txt'), 'w')

f2s = {line.strip('\n') for line in f2}

for line in f1:
    line = line.strip('\n')
    count = 0
    if line in f2s:
        count += 1
        print (str(line) + '\t' + str(1))
        o1.write(str(line) + '\t' + str(1) + '\n')
    if count == 0:
        print (str(line) + '\t' + str(0))
        o1.write(str(line) + '\t' + str(0) + '\n')

Embarrassing, I was opening the same file twice. Rookie.

  • 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-18T10:38:00+00:00Added an answer on June 18, 2026 at 10:38 am

    f2 is an iterator over your second file, and when that has been read, it’s exhausted.

    You can reset the iterator f2.seek(0, 0), but that’s not really the best way to go.

    Better put all the values from f2 into a set and then iterate over f1 only once:

    f2s = {line.strip('\n') for line in f2}
    
    for line in f1:
        name = line.strip('\n') # No need for \r\n
        if name in f2s:
            # etc.
    

    If you need to count the number of occurences of each line from f1 in f2, then you can use a Counter:

    from collections import Counter
    f2c = Counter(line.strip('\n') for line in f2)
    
    for line in f1:
        name = line.strip('\n')
        if name in f2c:
            count = f2c[name]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A relative noob to Python, I've successfully pulled a text file, out of a
I'm a complete Python noob. How can I remove two characters from the beginning
New to python, bear with me. I have two text files, each has a
I am a Python noob. I create a class as follows: class t1: x
I'm a noob in python. I'm kind of confused about how the python unit
Fair warning: I'm a big time noob. Please handle with kid gloves. Details: Python
I've just started programming, therefore I'm kinda noob. I'm trying to use python to
Python 3 really complicated the whole file reading process, when you have a binary
(Python) Given two numbers A and B. I need to find all nested groups
I'm attempting to convert the following statement to python (2.7): List<int> data = Entities.MyData.Select(x

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.