Search code examples
pythonpython-3.xxlsx

Creating a list of elements of .xlsx file single column


I'm stacking with a problem: I'm trying to create a list of floats in a single column of my .xlsx file. And I have this IndexError: tuple index out of range

x = []

wb=load_workbook('sample_data1(dynamics).xlsx')
ws = wb.active
columnX = ws['L']

for i in range(len(columnX)): 
    if columnX[i+1].value != '-': %Just because in some cells of this column I have '-'
        x.append(float(columnX[i+1].value)) 

When I try another version of code:

x = []

wb=load_workbook('sample_data1(dynamics).xlsx')
ws = wb.active
columnX = ws['L']

for i in range(len(columnX)): 
    if columnX[i].value != '-' or columnX[i].value != 'Point of Regard Right X [px]': %Just because first cell contain 'Point of Regard Right X [px]'
        x.append(float(columnX[i].value)) 

I have this error: ValueError: could not convert string to float: 'Point of Regard Right X [px].

So, can anybody help me, please?


Solution

  • pandas (with xlrd) usually does the trick for me:

    import pandas
    df = pandas.read_excel('sample_data1(dynamics).xlsx')
    col = 'REQUIRED COL'
    col_values_list = list(df[col])
    

    pandas will handle the conversion of the values to float.