Doubt in string comparison and list value for all rows

4

I have a table of 5 rows and 5 columns. For each column I have a name, for example: Name, Age, Sex, Weight, Height. And all 5 * 5 cells are filled.

I need the following formatting:

Idade
João - 15
José - 16
Maria - 21

Sexo
João - M
José - M
Maria - F

That is, for each cell information I need you to show me which person is related to this result.

I already ready the title (Age) and the value (16), but I can not link the name to each information, being:

Nome
JOão
JOsé
MAria

Idade
16
15
21

Sexo
M
M
F

I've created a variable that takes the value of the cell (x, y), but prints empty. Here is my code:

   for crange in sheet.merged_cells:                
      rlo, rhi, clo, chi = crange                    
      rranges[(clo,rlo)] = rhi - rlo;
    for col_index in range(sheet.ncols):
      linhas = iter(range(sheet.nrows));             
      nome_mol = ""                                   

      for row_index in linhas:
        if (sheet.cell_value(row_index,col_index) != ''): 
          valor = sheet.cell_value(row_index,col_index);
          if (sheet.cell_value(row_index,col_index) == "NOME"): 
            nome_mol = sheet.cell_value(row_index,col_index)   

            print "{} -- {} |{} |".format(nome_mol, valor) 
    
asked by anonymous 27.08.2014 / 16:24

1 answer

4
  

I'm assuming the first line contains the labels ("Name", "Age", ...) and the others contain data. If this is incorrect, please edit the question by clarifying the fact.

From what I understand, the column with the names [of people] is the first column (index 0 ), right? So you need to iterate over the others (% with% to% with%):

for col_index in range(1, sheet.ncols):
    print sheet.cell_value(0, col_index) # Imprime o nome da coluna

Likewise, the first row contains the column names, so you need to iterate over the others:

    for row_index in range(1, sheet.nrows):

The name of the person will then be in the first column of the line, and the attribute in question ("Age", "Sex", ...) will be the cross of the line with the column:

        nome = sheet.cell_value(row_index, 0)
        valor = sheet.cell_value(row_index, col_index)

Then you have to format these values and print them. In your code there is 1 left over:

        print "{} - {}".format(nome, valor) # Imprime o nome da pessoa e seu atributo

Complete code:

for col_index in range(1, sheet.ncols):
    print sheet.cell_value(0, col_index) # Imprime o nome da coluna
    for row_index in range(1, sheet.nrows):
        nome = sheet.cell_value(row_index, 0)
        valor = sheet.cell_value(row_index, col_index)
        print "{} - {}".format(nome, valor) # Imprime o nome da pessoa e seu atributo
    
28.08.2014 / 06:58