brian.p
brian.p

Reputation: 77

Search CSV file by row column

Hi I was wondering if there is a way to search a CSV file without importing CSV module to search by row and column.

Also how could I use re.findall to find instances of a word in a column or row

For example the genre is Horror and the movie title starts with The ..so I'd need to find the total amount of "The" movies in the horror genre

Thanks

Upvotes: 1

Views: 124

Answers (1)

pacdev
pacdev

Reputation: 581

This may be not the best solution but it works. Let's imagine you have a films.csv file with film name and author like this:

title, author
The dogs, author1
The cats, author2
Cars, author1
Houses, author3
The apples, Jobs

If you want to count the number of occurrences of film name starting by "The":

with open('films.csv', 'r') as f:
    data = f.read()
n = len(re.findall("The*", data))   

Upvotes: 1

Related Questions