Click here to Skip to main content
15,901,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)
Posted
Comments
Richard MacCutchan 30-Jan-17 4:51am    
Try checking for None at the beginning of each loop so you eliminate all such rows immediately that you find them.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900