Reputation: 13
Very green python user here. I have a .csv file with hundreds of wines, their price and rating, and the wineries that sell a particular wine. I am trying to write a function that returns the number off all the wineries for a given wine, shown below:
def all_wineries(wine):
wineries = []
for i in range(len(csv_data)):
if wine.lower() in cell(i, "variety").lower():
wineries.append(cell(i, "winery"))
return list(set(wineries))
x = all_wineries("Pinot Noir")
len(x)
However, I failed to notice that some wines are hyphenated (Ex. "Pinot Noir-Gamay) and those are being included in my final answer. How can I ignore those wines that are not exactly what I type into my function?
Upvotes: 1
Views: 66
Reputation: 1845
I'm elaborating on Ed Ward's comment here.
wine.lower().strip() == cell(i, "variety").lower().strip()
The lower() function converts the string to lowercase. The strip() function removes any whitespace (spaces, tabs) from the beginning and end of the string. Use these two functions on both the wine you are looking for and the wine you are looking at. The two wines will only match if they are exactly the same (no hyphens).
def all_wineries(wine):
wineries = []
for i in range(len(csv_data)):
if wine.lower().strip() == cell(i, "variety").lower().strip()
wineries.append(cell(i, "winery"))
return list(set(wineries))
x = all_wineries("Pinot Noir")
len(x)
Upvotes: 1