Using a simple "if "blank" in string" statement i would like to find an exact word in a csv column.
with open("example.csv","r") as file
reader=csv.reader(file)
string=""
for row in reader:
if "Hello" in row[0]:
string+=row[0]
print(string)
If the csv has "Hello World"
in row[0] string will equal "Hello World" but I only want string to +=row[0]
if specifically only "Hello" is in row[0] without "World".
In conclusion, I'm hoping the code prints nothing unless exactly "Hello" is in row[0].
You can check if one string is exactly the same as another using ==
:
if row[0] == "Hello":
That will find you all the rows with an exact value. You should only use in
if you are looking for part of a string.
If you want to find "Hello" when it is part of a string except when it is followed by "World", then you could try
if "Hello" in row[0] and not "Hello World" in row[0]:
For anything more complicated, you'd need to look into regular expressions.