I have an excel sheet with cities (alphabetized) and two columns of corresponding organizations, and am trying to create a new master list in python, with sublists containing all of the organizations in a certain city. Yet when I test this code, I'm getting sublists that contain 'None' even though I tried everything I could think of to filter nonetypes out. For some reason, it seems like some of the loops in my code are never running.
Ultimately, I plan to use fuzzywuzzy to use fuzzy matching to estimate the number of unique organizations in each city (whoever entered them did a terrible job, so I'm just trying to get an estimate).
If you can help, thanks!
What I have tried:
from openpyxl import load_workbook
wb = load_workbook(filename = '/Users/Me/Downloads/example.xlsx', read_only = True, data_only = True)
ws = wb['Sheet1']
masterList = []
nextList = []
for i in range(2, ws.max_row):
f = i + 1
try:
#I was having lots of problems with errors, and this seemed to solve that.
if (str((ws.cell(row = i, column = 1).value).lower())) == (str((ws. cell(row = f, column = 1).value).lower())):
#checking if it's still the same city
if (ws.cell(row = i, column = 2).value) != None and str((ws.cell(row = i, column = 2).value)) != "None" :
#Making sure there are no nonetypes
try:
nextList.append(str(ws.cell(row = i, column = 2).value))
except TypeError or AttributeError or ValueError:
continue
elif (ws.cell(row = i, column = 3).value) != None and str((ws.cell(row = i, column = 3).value)) != "None" :
#There are two columns with organizations
try:
nextList.append(str(ws.cell(row = i, column = 3).value))
except TypeError or AttributeError or ValueError:
continue
else:
#in testing I realized that this part of the loop never ran, though it should have
continue
else:
try:
if (ws.cell(row = i, column = 2).value) != None:
nextList.append(str(ws.cell(row = i, column = 2).value))
print("C")
elif (ws.cell(row = i, column = 3).value) != None:
nextList.append(str(ws.cell(row = i, column = 2).value))
print("D")
else:
continue
except TypeError or AttributeError or ValueError:
continue
if nextList!= []:
masterList.append(list(set(nextList)))
nextList = []
else:
continue
except AttributeError:
continue
print(masterList)