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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:33:33+00:00 2026-05-29T07:33:33+00:00

I am getting the following error with Python 3.1.4 which used to work well

  • 0

I am getting the following error with Python 3.1.4 which used to work well in Python 2.7.2.

TypeError: Can't convert 'list' object to str implicitly. I get the error on the if statement. Please let me know how to fix this. Thanks!

In

for word in keywords: # Iterate through keywords
    if re.search(r"\b"+word+r"\b",line1):           #Search kewords in the input line

Update1:

I am trying to create a list from keywords which is in a file. Each line has one keyword. Am I reading the file properly?

keyword_file=r"KEYWORDS.txt"
f0=open(keyword_file,'r')
keywords = map(lambda a: a.split('\n'),map(str.lower, f0.readlines()))

keyword file contains:

Keyword1
Keyword2
.
.
.
Keywordn

I want a list called keywords = ['Keyword1','Keyword2',...,'Keywordn']

  • 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-29T07:33:34+00:00Added an answer on May 29, 2026 at 7:33 am

    You split the lines although they have already been split by readlines(). This should work:

    # actually no need for readline() here, the file object can be
    # directly used to iterate over the lines
    keywords = (line.strip().lower() for line in f0)
    # ...
    for word in keywords:
      if re.search(r"\b"+word+r"\b",line1):
    

    What’s used here is a generator expression. You should learn about those, they are quite handy, as well as about list comprehensions which can often be used to replace map and filter.

    Note that it might be more performant to create the regular expression before the loop, like this:

    keywords = (line.strip() for line in f0)
    # use re.escape here in case the keyword contains a special regex character
    regex = r'\b({0})\b'.format('|'.join(map(re.escape, keywords)))
    # pre-compile the regex (build up the state machine)
    regex = re.compile(regex, re.IGNORECASE)
    
    # inside the loop over the lines
    if regex.search(line1)
      print "ok"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Python, I'm getting the following error: UnboundLocalError: local variable 'total' referenced before assignment
I'm getting the following error: Template error In template /home/mo/python/django/templates/yoga/index.html, error at line 1
I'm getting the following error when using the multiprocessing module within a python daemon
I'm trying to use Python's xml.dom.minidom, and I'm getting the following error: >>> from
python version 2.4 # pip install psycopg2 Getting the following error: Downloading/unpacking psycopg2 Running
Starting with urllib2 and feedparser libraries in Python I'm getting the following error most
Am getting following error message on calling WCF service: The formatter threw an exception
I am getting following error when I am trying to get WebResponse using WebResponse
I am getting following error message when using Doctrine ORM in Codeigniter. ( !
SOLVED in the last answer im getting following error, dunno where and why? cause

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.