Read rows and columns by Python, by excel

Asked

Viewed 21,806 times

4

My doubt is if there is any resource for me to be able to read excel rows and columns. For example: I have experimental data in excel containing header in the first row having about 12 columns of data, so [1:12] would be just the header, and about 30 rows of numerical data, I wanted to know exactly that, as it takes only of the data within that matrix, and then select each column and form a list?

1 answer

3


Yes, there is the package xlrd who reads Excel files for you.

Example:

import xlrd
book = xlrd.open_workbook("meuarquivo.xls")
print "Número de abas: ", book.nsheets
print "Nomes das Planilhas:", book.sheet_names()
sh = book.sheet_by_index(0)
print(sh.name, sh.nrows, sh.ncols)
print("Valor da célula D30 é ", sh.cell_value(rowx=29, colx=3))
for rx in range(sh.nrows):
    print(sh.row(rx))

I took the example from here.

See also this site.

  • For example, in this my spreadsheet in excel has 7 pages, before I would have to select some, or it will always take only the first?

  • As an example, you select the page using something like sh = book.sheet_by_index(0), where 0 is the index of the page.

  • 1

    I was a little lost on this command, now it’s clearer, because sheet is page. I’m a beginner there I always miss these little things.

  • I ran some tests, and it gave me some unsatisfying results. First because I can not select for example only column 3 of sheet 3 and then select another column, because my goal is to select this data excluding the header and form a new list, to then form a chart.

  • Well, there’s no magic in it. I showed you the beginning of the path. The question says nothing about graphics. Anyway, with some research and some written code, you can arrive at a question that is closer than you want.

  • No, the Graphics I already know how to do, the problem is to select the files, if I can select these files and put them in list form I can make the chart from these data. 'Cause the way you’re at least reading everything, you can read all the blank data.

  • Yes, because as I told you, you wanted an answer to read the Excel data. The refinement of logic is not part of the question. This is with you.

Show 2 more comments

Browser other questions tagged

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