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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:43:44+00:00 2026-05-24T08:43:44+00:00

There’s a text file that I’m reading line by line. It looks something like

  • 0

There’s a text file that I’m reading line by line. It looks something like this:

3

3

67

46

67

3

46

Each time the program encounters a new number, it writes it to a text file. The way I’m thinking of doing this is writing the first number to the file, then looking at the second number and checking if it’s already in the output file. If it isn’t, it writes THAT number to the file. If it is, it skips that line to avoid repetitions and goes on to the next line. How do I do this?

  • 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-24T08:43:44+00:00Added an answer on May 24, 2026 at 8:43 am

    Instead of checking output file for the number if it was already written it is better to keep this information in a variable (a set or list). It will save you on disk reads.

    To search a file for numbers you need to loop through each line of that file, you can do that with for line in open('input'): loop, where input is the name of your file. On each iteration line would contain one line of input file ended with end of line character ‘\n’.

    In each iteration you should try to convert the value on that line to a number, int() function may be used. You may want to protect yourself against empty lines or non-number values with try statement.

    In each iteration having the number you should check if the value you found wasn’t already written to the output file by checking a set of already written numbers. If value is not in the set yet, add it and write to the output file.

    #!/usr/bin/env python                                                           
    numbers = set() # create a set for storing numbers that were already written       
    out = open('output', 'w') # open 'output' file for writing                      
    for line in open('input'): # loop through each line of 'input' file             
        try:                                                                        
            i = int(line) # try to convert line to integer                          
        except ValueError:  # if conversion to integer fails display a warning         
            print "Warning: cannot convert to number string '%s'" % line.strip()       
            continue # skip to next line on error                                   
        if i not in numbers: # check if the number wasn't already added to the set  
            out.write('%d\n' % i) # write the number to the 'output' file followed by EOL
            numbers.add(i) # add number to the set to mark it as already added
    

    This example assumes that your input file contains single numbers on each line. In case of empty on incorrect line a warning will be displayed to stdout.

    You could also use list in the above example, but it may be less efficient.
    Instead of numbers = set() use numbers = [] and instead of numbers.add(i): numbers.append(i). The if condition stays the same.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
There is a text file with 1,000,000 lines of code floating around and Ctrl+F
There are things like f.call(...) f.apply(...) But then there's this (1, alert)('Zomg what is
There's a common error that gets thrown by the Visual C Runtime: This application
There are posts that touch on this question, but no one that I can
There seem to be very different opinions about using transactions for reading from a
There seems to be a similar question to this on here but with the
There is nothing like the 43rd day of your life spent tracking down issues
There are several shading languages available today like GLSL, HLSL, CG, which one to
There's a bug in FireBug: I accidentally clicked where the line numbers are, which

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.