-2

I have used OpenPyXL to read excel file. I want a list of names extracted from excel file in 4th column and all rows. But problem is that after certain rows the values are not assigned.So it is storeing it as 'None' in list.But i don't want None in my list , just the names.

#Loading excel file
file_path = '/Volumes/DATA/Project/NLP/Web-scraping/Client_Dataset.xlsx'
data = openpyxl.load_workbook(filename=file_path)
# ws = data.get_sheet_by_name('Data - Sheet1 - Data - Sheet1')

data_excel = data.active
#print(data)
list_ = []
for cell in data_excel['D']:
    if 'Company' == cell.value:
        continue
    list_.append(cell.value)

print(list_)

Output of above code

['Addepar', 'ADDI', 'Agicap', 'Airbase', 'Airwallex', 'Alan', 'Albert', 'Alchemy', 'Alloy', 'AlphaSense', 'Alto IRA', 'Amber Group', 'Amount', 'Anchorage Digital', 'Arturo', 'At-Bay', 'Atom Finance', 'Autobooks', 'AvidXchange', 'Balance', 'Belvo', 'Bestow', 'Betterment', 'BharatPe', 'Bitcoin Suisse', 'Bitpanda', 'Bitso', 'Bitwise Asset Management', 'Blockchain.com', 'Blockdaemon', 'BlockFi', 'BlueVine', 'Bolt', 'Borrowell', 'Bought By Many\nManyPets (new name)', 'Brex', 'Brightflag', 'C2FO', 'Cambridge Mobile Telematics', 'Capchase', 'Capital Float',None, None, None, None, None, None, None, None, None, None, None, None]

Expected outcome that i need is.

['Addepar', 'ADDI', 'Agicap', 'Airbase', 'Airwallex', 'Alan', 'Albert', 'Alchemy', 'Alloy', 'AlphaSense', 'Alto IRA', 'Amber Group', 'Amount', 'Anchorage Digital', 'Arturo', 'At-Bay', 'Atom Finance', 'Autobooks', 'AvidXchange', 'Balance', 'Belvo', 'Bestow', 'Betterment', 'BharatPe', 'Bitcoin Suisse', 'Bitpanda', 'Bitso', 'Bitwise Asset Management', 'Blockchain.com', 'Blockdaemon', 'BlockFi', 'BlueVine', 'Bolt', 'Borrowell', 'Bought By Many\nManyPets (new name)', 'Brex', 'Brightflag', 'C2FO', 'Cambridge Mobile Telematics', 'Capchase', 'Capital Float']

1 Answers1

1

Really simple check if cell.value is not None and it will only append the values that are not equal to None:

#Loading excel file
file_path = '/Volumes/DATA/Project/NLP/Web-scraping/Client_Dataset.xlsx'
data = openpyxl.load_workbook(filename=file_path)
# ws = data.get_sheet_by_name('Data - Sheet1 - Data - Sheet1')

data_excel = data.active
#print(data)
list_ = []
for cell in data_excel['D']:
    if 'Company' == cell.value:
        continue
    if cell.value is not None: ## <- Et voila
        list_.append(cell.value)

print(list_)
Diurnambule
  • 73
  • 1
  • 4