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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:33:27+00:00 2026-06-11T05:33:27+00:00

I want to find strings listed in list.txt (one string per line) in another

  • 0

I want to find strings listed in list.txt (one string per line) in another text file in case I found it print ‘string,one_sentence’ in case didn’t find ‘string,another_sentence’. I’m using following code, but it is finding only last string in the strings list from file list.txt. Cannot understand what could be the reason?

data = open('c:/tmp/textfile.TXT').read()
for x in open('c:/tmp/list.txt').readlines():
    if x in data:
        print(x,',one_sentence')
    else:
        print(x,',another_sentence')
  • 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-11T05:33:28+00:00Added an answer on June 11, 2026 at 5:33 am

    When you read a file with readlines(), the resulting list elements do have a trailing newline characters. Likely, these are the reason why you have less matches than you expected.

    Instead of writing

    for x in list:
    

    write

    for x in (s.strip() for s in list):
    

    This removes leading and trailing whitespace from the strings in list. Hence, it removes trailing newline characters from the strings.

    In order to consolidate your program, you could do something like this:

    with open('c:/tmp/textfile.TXT') as f:
        haystack = f.read()
    
    if not haystack:
        sys.exit("Could not read haystack data :-(")
    
    with open('c:/tmp/list.txt') as f:
        for needle in (line.strip() for line in f):
            if needle in haystack:
                print(needle, ',one_sentence')
            else:
                print(needle, ',another_sentence')
    

    I did not want to make too drastic changes. The most important difference is that I am using the context manager here via the with statement. It ensures proper file handling (mainly closing) for you. Also, the ‘needle’ lines are stripped on the fly using a generator expression. The above approach reads and processes the needle file line by line instead of loading the whole file into memory at once. Of course, this only makes a difference for large files.

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

Sidebar

Related Questions

I want to use regular expression to find strings in a file that have
I have two MySQL tables, and I want to find and replace text strings
I want to find all img tags in a string of text and put
I have two IList<string> a and b. I want to find out what strings
I want to process a String, in which I want to find multiple strings,
I want to find all strings containing at least 1 Cyrillic character (basically /.*[А-я].*/)
I want to find out how I can store small strings in an array
I have a string like 'apples'. I want to find this string, and I
i have following urls, which i want to find a string and remove that
I want to find if a string contains a repeated sequence of a known

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.