Search code examples
pythonexcelpython-3.xxlrd

Data append to list using XLRD


I am able to import data of rows in a particular column of certain sheet name in to a python list. But, the list is looking like Key:Value formatted list (not the one I need).

Here is my code:

import xlrd

excelList = []

def xcel(path):
    book = xlrd.open_workbook(path)
    impacted_files = book.sheet_by_index(2)
    for row_index in range(2, impacted_files.nrows):
        #if impacted_files.row_values(row_index) == 'LCR':
        excelList.append(impacted_files.cell(row_index, 1))
    print(excelList)

if __name__ == "__main__":
    xcel(path)

The output is like below:

[text:'LCR_ContractualOutflowsMaster.aspx', text:'LCR_CountryMaster.aspx', text:'LCR_CountryMasterChecker.aspx', text:'LCR_EntityMaster.aspx', text:'LCR_EntityMasterChecker.aspx', text:'LCR_EscalationMatrixMaster.aspx',....]

I want the list to have just values. Like this...

['LCR_ContractualOutflowsMaster.aspx', 'LCR_CountryMaster.aspx', 'LCR_CountryMasterChecker.aspx', 'LCR_EntityMaster.aspx', 'LCR_EntityMasterChecker.aspx', 'LCR_EscalationMatrixMaster.aspx',...]

I've tried pandas too (df.value.tolist() method). Yet the output is not what I visualize.

Please suggest a way.

Regards


Solution

  • You are accumulating a list of cells, and what you are seeing is the repr of each cell in your list. Cell objects have three attributes: ctype is an int that identifies the type of the cell's value, value (which which is a Python rtype holding the cell's value) and xf_index. If you want only the values then try

    excelList.append(impacted_files.cell(row_index, 1).value)
    

    You can read more about cells in the documentation.