How to transfer data from tables to python?

Asked

Viewed 1,224 times

-1

Hello, I have a table with a lot of data, a lot of data even from Astrophysics, are tables with approximately 20 columns and more than 1000 rows. I need to send this table with this data (contained in the columns and rows) to python 2.7 on my linux but I can’t do that. Would you please help me?

  • 1

    Your question is a little vague, without objectivity. Have you thought about using the Numpy for that? Look at this "Quickstart": https://docs.scipy.org/doc/numpy-dev/user/quickstart.html

  • 2

    How to send to python?

  • 1

    In what format is this table?

2 answers

0

As you did not mention file format I will assume xlsx.

Making a pseudo-code in EAFP I imagine you can do something like that (as you didn’t mention, I imagine you’re not using any data analysis library like http://pandas.pydata.org/ for example) - as your data is scientific it is likely that a simple reading and iteration of tables will not suffice, however:

import xlrd

def open_file(caminho_de_arquivo):
    """
    Definição da função que irá ler e criar no seu runtime a tabela equivalente
    ao arquivo
    """
livro = xlrd.open_workbook(caminho_de_arquivo)

# exibir no console o número de planilhas
print livro.nsheets

# exibir o nome das planilhas
print livro.sheet_names()

# obter a primeira planilha, assinalando-a a uma variável local
primeira_planilha = book.sheet_by_index(0)

# exibir a primeira coluna da primeira planilha
print primeira_planilha.row_values(0)

# obter e exibir uma célula da primeira planilha
cell = primeira_planilha.cell(0,0)
print cell
print cell.value

# ler um slice de linha
print primeira_planilha.row_slice(rowx=0,
                            start_colx=0,
                            end_colx=2)


#----------------------------------------------------------------------
if __name__ == "__main__":
    path = "caminho.xls"
    open_file(path)with open('filepath.xlsx', 'r') as importer:

From this you can iterate on the spreadsheet as you want in a simple way:

cells = primeira_planilha.row_slice(rowx=0,
                              start_colx=0,
                              end_colx=2)
for cell in cells:
    print cell.value

Tutorial removed and translated from post http://www.blog.pythonlibrary.org/2014/04/30/reading-excel-spreadsheets-with-python-and-xlrd/

0

If you refer to passing the data as parameters to your Python script, on the command line, I would suggest two alternatives:

  1. Pass as the path to a file: python meuscript.py /caminho/para/tabela.txt
  2. Pass as input (stdin): python meyscript.py < /caminho/para/tabela.txt

The second alternative is more flexible as it will also allow uses such as: python gerar-tabela.py param1 param2 | python meuscript.py

Browser other questions tagged

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