Skip to content
Advertisement

Check whether string is in CSV

I want to search a CSV file and print either True or False, depending on whether or not I found the string. However, I’m running into the problem whereby it will return a false positive if it finds the string embedded in a larger string of text. E.g.: It will return True if string is foo and the term foobar is in the CSV file. I need to be able to return exact matches.

JavaScript

I’ve looked at using mmap, re and csv module functions, but I haven’t got anywhere with them.

EDIT: Here is an alternative method:

JavaScript

Advertisement

Answer

when you look inside a csv file using the csv module, it will return each row as a list of columns. So if you want to lookup your string, you should modify your code as such:

JavaScript

but as it is a csv file, you might expect the username to be at a given column:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement