How to transfer table data to python?

-1

Hello, I have a table with lots of data, a lot of Astrophysics data, 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 in my linux but I can not do that. Could you please help me out?

    
asked by anonymous 13.10.2016 / 21:53

2 answers

0

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

  • Pass as the path to a file: python meuscript.py /caminho/para/tabela.txt
  • Pass as input (stdin): python meyscript.py < /caminho/para/tabela.txt
  • The second alternative is more flexible because it will also allow uses like: python gerar-tabela.py param1 param2 | python meuscript.py

        
    17.10.2016 / 18:04
    0

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

    By doing a pseudo-code in EAFP I imagine you can do something like this (as you have not mentioned, I imagine you are not using any data analysis library like for example) - as their data is scientific it is likely that a simple reading and iteration of tables is not enough, 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 over 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 taken and translated from post link

        
    19.10.2016 / 12:48