'python startswith() doesnt work on function
Im making a search function in python where it looks inside the CSV file and returns the similar data being searched. I am not using a CSV file and I cant seem to make the startswith work with it.
Here's my code:
def search(i):
data = []
name = i.capitalize()
with open('data.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
data.append(row)
fname = [x[0] for x in data]
if name in fname:
for i in range(0, len(data)):
if data[i][0] == name or data[i][0].startswith(name):
return(data[i])
Sample Data(CSV)
Name1,Male,3321,[email protected]
Name2,Male,1231,[email protected]
Name3,Female,3421,[email protected]
Whenever i search the full name of the person, it displays a result but not when im only searching for the first characters of the name, it doesnt work. Any help is appreciated!
Edit: What im passing through the search function is the keyword typed inside the search box
Solution 1:[1]
In data[i][0].startswith(name) I think it should be name.startswith(data[i][0])
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 | The Thonnu |
