'Python - Searching input variable in csv file
I'm trying to develop a code where if the input variable is in the csv file, the programa should print "Encontrei" (portuguese word for "found") and if not , print ("Não encontrei"). However, this isn´t working, in the input I'm wrighting a word that I know it´s in the csv file, but I´m always getting: "Não encontrei". I tryed 2 different approaches and I´m getting the same result.
1st code:
def pilar():
pilar = input("Perfil do pilar:")
csv_file=csv.reader(open(r"C:\Users\tomas\Documents\ISEP\5º Ano\TESE\PROGRAMA\PERFIS.csv"))
for row in csv_file:
if pilar in csv_file:
print("Pilar: ", pilar)
else:
print("Não encontrei")
pilar()
2nd code:
import csv
with open(r"C:\Users\tomas\Documents\ISEP\5º Ano\TESE\PROGRAMA\PERFIS.csv") as perfis:
csv_dict_reader = csv.DictReader(perfis)
for row in perfis:
pilar = input("Pilar: ")
if pilar in perfis:
print("Encontrei")
else:
print("Não encontrei")
Solution 1:[1]
Change this:
for row in csv_file:
if pilar in csv_file:
To this:
for row in csv_file:
if pilar in row:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | at54321 |
