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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:52:47+00:00 2026-05-22T18:52:47+00:00

Hi I’m new to Python, so this may come across as a simple problem

  • 0

Hi I’m new to Python, so this may come across as a simple problem but I’ve been searching through Google many times and I can’t seem to find a way to overcome it.
Basically I have a list of strings, taken from a CSV file. And I have another list of strings in a text file. My job is to see if the words from my text file are in the CSV file.

Let’s say this is what the CSV file looks like (it’s made up):

  name,author,genre,year
  Private Series,Kate Brian,Romance,2003
  Mockingbird,George Orwell,Romance,1956
  Goosebumps,Mary Door,Horror,1990
  Geisha,Mary Door,Romance,2003

And let’s say the text file looks like this:
Romance
2003

What I’m trying to do is, create a function which returns the names of a book which have the words “Romance” and “2003” in them. So in this case, it should return “Private Series” and “Geisha” but not “Mockingbird”. But my problem is, it doesn’t seem to return them. However when I change my input to “Romance” it returns all three books with Romance in them. I assume it’s because “Romance 2003” aren’t together because if I change my input to “Mary Door” both “Goosebumps” and “Geisha” show up. So how can I overcome this?

Also, how do I make my function case insensitive?

Any help would be much appreciated 🙂

  • 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-22T18:52:48+00:00Added an answer on May 22, 2026 at 6:52 pm
    import csv
    
    def read_input(filename):
        f = open(filename)
        return csv.DictReader(f, delimiter = ',')
    
    def search_filter(src, term):
        term = term.lower()
        for s in src:
            if term in map(str.lower, s.values()):
                yield s
    
    def query(src, terms):
        terms = terms.split()
        for t in terms:
            src = search_filter(src, t)
        return src
    
    def print_query(q):    
        for row in q:
            print row
    

    I tried to split the logic into small, re-usable functions.

    First, we have read_input which takes a filename and returns the lines of a CSV file as an iterable of dicts.

    The search_filter filters a stream of results with the given term. Both the search term and the row values are changed to lowercase for the comparison to achieve case-independent matching.

    The query function takes a query string, splits it into search terms and then makes a chain of filters based on the terms and returns the final, filtered iterable.

    >>> src = read_input("input.csv")
    >>> q = query(src, "Romance 2003")
    >>> print_query(q)
    {'genre': 'Romance', 'year': '2003', 'name': 'Private Series', 'author': 'Kate Brian'}
    {'genre': 'Romance', 'year': '2003', 'name': 'Geisha', 'author': 'Mary Door'}
    

    Note that the above solution only returns full matches. If you want to e.g. return the above matcher with the search query “Roman 2003”, then you can use this alternative version of search_filter:

    def search_filter(src, term):
        term = term.lower()
        for s in src:
            if any(term in v.lower() for v in s.values()):
                yield s
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
I want use html5's new tag to play a wav file (currently only supported
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm looking for suggestions for debugging... If you view this site in Firefox or
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,

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.