Skip to main content

Searching CSV file for exact Check whether string - Python 3is in CSV

G'day everyone,

I've read all over the place on this topic, but am yetI want to find a solution that works for me. Here's what I've got so far. As you can see, it will search thea CSV file, and print either TrueTrue or FalseFalse, depending on whether or not it findsI found the usernamestring. However, I'm running into the problem whereby it will return a false positive if it finds the usernamestring embedded in a larger string of text. EgE.g.: It will return TrueTrue if usernamestring is foofoo and the term foobarfoobar 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")

From other questions on here, I've looked at using mmapmmap, rere and csvcsv module functions, but I haven't got anywhere with them. I've posted the above code to show the sort of level I'm currently at with Python (very much a beginner). Any points in the right direction would be greatly appreciated! If you need any further detail, just let me know.

EDIT: I thought I'd add an example ofHere is an alternatealternative method I've been pursuing. Again, I'm a beginner and am using this particulat syntax for the first time :)

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)

Searching CSV file for exact string - Python 3

G'day everyone,

I've read all over the place on this topic, but am yet to find a solution that works for me. Here's what I've got so far. As you can see, it will search the CSV file, and print either True or False, depending on whether or not it finds the username. However, I'm running into the problem whereby it will return a false positive if it finds the username embedded in a larger string of text. Eg: will return True if username 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")

From other questions on here, I've looked at using mmap, re and csv module functions, but I haven't got anywhere with them. I've posted the above code to show the sort of level I'm currently at with Python (very much a beginner). Any points in the right direction would be greatly appreciated! If you need any further detail, just let me know.

EDIT: I thought I'd add an example of an alternate method I've been pursuing. Again, I'm a beginner and am using this particulat syntax for the first time :)

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)

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.

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)
Source Link
jars121
  • 1.2k
  • 3
  • 20
  • 36

Searching CSV file for exact string - Python 3

G'day everyone,

I've read all over the place on this topic, but am yet to find a solution that works for me. Here's what I've got so far. As you can see, it will search the CSV file, and print either True or False, depending on whether or not it finds the username. However, I'm running into the problem whereby it will return a false positive if it finds the username embedded in a larger string of text. Eg: will return True if username 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")

From other questions on here, I've looked at using mmap, re and csv module functions, but I haven't got anywhere with them. I've posted the above code to show the sort of level I'm currently at with Python (very much a beginner). Any points in the right direction would be greatly appreciated! If you need any further detail, just let me know.

EDIT: I thought I'd add an example of an alternate method I've been pursuing. Again, I'm a beginner and am using this particulat syntax for the first time :)

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)