Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether string is in CSV

Tags:

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.

username = input()  if username in open('Users.csv').read():     print("True") else:     print("False") 

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:

import re import csv  username = input()  with open('Users.csv', 'rt') as f:      reader = csv.reader(f)      for row in reader:           re.search(r'\bNOTSUREHERE\b', username) 
like image 286
jars121 Avatar asked Jun 25 '13 22:06

jars121


People also ask

Is everything a string in csv?

CSV is string data. If your data is normalized you can do things like attempt to convert to a numeric; if it works, it's a number, if it fails, it's a string. It will fail.

What is the use of DictReader() function?

DictReader() class can be used to read a CSV file as a dictionary.

How do I search a csv file?

To search a CSV file, the best option is to use csvReadArray to import the whole CSV file into a 2-dimensional array. Then you can use the Javascript “findIndex” command to search the array. The findIndex() method returns the index of the first element in an array that pass a test (provided as a function).


2 Answers

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:

import csv  username = input()  with open('Users.csv', 'rt') as f:      reader = csv.reader(f, delimiter=',') # good point by @paco      for row in reader:           for field in row:               if field == username:                   print "is in file" 

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

with open('Users.csv', 'rt') as f:      reader = csv.reader(f, delimiter=',')      for row in reader:           if username == row[2]: # if the username shall be on column 3 (-> index 2)               print "is in file" 
like image 122
zmo Avatar answered Sep 21 '22 20:09

zmo


You should have a look at the csv module in python.

is_in_file = False with open('my_file.csv', 'rb') as csvfile:     my_content = csv.reader(csvfile, delimiter=',')     for row in my_content:         if username in row:             is_in_file = True print is_in_file 

It assumes that your delimiter is a comma (replace with the your delimiter. Note that username must be defined previously. Also change the name of the file. The code loops through all the lines in the CSV file. row a list of string containing each element of your row. For example, if you have this in your CSV file: Joe,Peter,Michel the row will be ['Joe', 'Peter', 'Michel']. Then you can check if your username is in that list.

like image 35
Paco Avatar answered Sep 25 '22 20:09

Paco