Removing strings from within a list

Asked

Viewed 54 times

2

Hello I have a list I get from a file xls through the library xlrd

workbook = xlrd.open_workbook(r"C:\Users\Expedição\Videos\CSV\produtos_filtrado.xls") # Escolhe o arquivo a ser lido.

worksheet = workbook.sheet_by_index(0) #Escolha a aba a ser lida. 

for i in range(1,worksheet.nrows): #itere sobre os itens da aba 
    lista = worksheet.row(i)
    print(lista)

the result of print:

[text:'2746', text:'8512.20.21', text:'2 - Estrangeira - Adquirida no mercado interno, exceto a indicada no código 7', text:'SL-121410', text:'6949999876781']
[text:'2747', text:'8512.20.21', text:'2 - Estrangeira - Adquirida no mercado interno, exceto a indicada no código 7', text:'SL-121410CR', text:'6949999876798']
[text:'2794', text:'8512.20.21', text:'2 - Estrangeira - Adquirida no mercado interno, exceto a indicada no código 7', text:'SL-121510', text:'6949999876811']
[text:'2795', text:'8512.20.21', text:'2 - Estrangeira - Adquirida no mercado interno, exceto a indicada no código 7', text:'SL-043210', text:'6949999876804']

as it is possible to observe there is a text: before the data I need, my question is how to remove this text:?

1 answer

2


It is that so you are creating a list of XLRD objects, do this way to recover only the values:

lista = list()
for i in range(1,worksheet.nrows):
    lista.append(
        [j.value for j in worksheet.row(i)]
    )

print(lista)

And he will assemble the list only with the values of each cell.

  • I only have a doubt that for leaves the last value in the list? knows why?

  • Hmmm, I based on your example, I made an edit on the answer and now it solves the problem.

  • Now I understand thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.