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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:25:09+00:00 2026-05-25T00:25:09+00:00

Text file 1 has the following format: ‘WORD’: 1 ‘MULTIPLE WORDS’: 1 ‘WORD’: 2

  • 0

Text file 1 has the following format:

'WORD': 1
'MULTIPLE WORDS': 1
'WORD': 2

etc.

I.e., a word separated by a colon followed by a number.

Text file 2 has the following format:

'WORD'
'WORD'

etc.

I need to extract single words (i.e., only WORD not MULTIPLE WORDS) from File 1 and, if they match a word in File 2, return the word from File 1 along with its value.

I have some poorly functioning code:

def GetCounts(file1, file2):
    target_contents  = open(file1).readlines()  #file 1 as list--> 'WORD': n
    match_me_contents = open(file2).readlines()   #file 2 as list -> 'WORD'
    ls_stripped = [x.strip('\n') for x in match_me_contents]  #get rid of newlines

    match_me_as_regex= re.compile("|".join(ls_stripped))   

    for line in target_contents:
        first_column = line.split(':')[0]  #get the first item in line.split
        number = line.split(':')[1]   #get the number associated with the word
        if len(first_column.split()) == 1: #get single word, no multiple words 
            """ Does the word from target contents match the word
            from match_me contents?  If so, return the line from  
            target_contents"""
            if re.findall(match_me_as_regex, first_column):  
                print first_column, number

#OUTPUT: WORD, n
         WORD, n
         etc.

Because of the use of regex, the output is shotty. The code will return ‘asset, 2’, for example, since re.findall() will match ‘set’ from match_me. I need to match the target_word with the entire word from match_me to block the bad output resulting from partial regex matches.

  • 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-25T00:25:10+00:00Added an answer on May 25, 2026 at 12:25 am

    If file2 is not humongous, slurp them into a set:

    file2=set(open("file2").read().split())
    for line in open("file1"):
        if line.split(":")[0].strip("'") in file2:
            print line
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Supposedly I have this line in my text file which has the following format.
I currently have a text file that has the following: 1 Commercial & Enterprise
The text file has hundreds of these entries (format is MT940 bank statement) {1:F01AHHBCH110XXX0000000000}{2:I940X
Let's say I have an input text file of the following format: Section1 Heading
I have a file which has contents on every line following this format (A,
I had an interview question asking this: text file has following lines> 1: A
I have a .txt file that has the following details: ID^NAME^DESCRIPTION^IMAGES 123^test^Some text goes
I have a text file which has the following data {i:1, j:2} {k:3, l:10}
I have progress.js file which has the following code $('#text_area_input').keyup(function() { var text_area_box =$(this).val();//Get
I'm using a preformatted text file as a template for emails. The file has

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.