How to remove spaces at the beginning and end of an XLS output

3

I have the code below, so it opens a spreadsheet in XLS and shows me the header. however I would like the left and right spaces to be removed and the internal spaces to be replaced by underline is there any function in xlrd that does this?

   import xlrd

    def xlread(arq_xls):

        xls = xlrd.open_workbook(arq_xls)
        # Pega a primeira planilha do arquivo
        plan = xls.sheets()[0]

        # Para i de zero ao numero de linhas da planilha
        for i in xrange(plan.nrows):
            # Le os valores nas linhas da planilha
            yield plan.row_values(0)

    for linha in xlread("teste.xls"):
        print linha

Example Output;

[u'Cod.Lab.', u'Laudo', u'Cliente', u'Fazenda', u'Talh\xe3o', u'Profundidade', u'Descri\xe7\xe3o', u' pH          ', u'pH', u' pH           ', u'CE', u'MO ', u'P resina', u'S-SO4', u'K (res)', u'Na', u'Ca', u'Mg', u'Al'

As you can see where it is unconfigured, there are several spaces in the "pH".

    
asked by anonymous 18.11.2015 / 15:48

2 answers

4

To remove the blanks before and after the text, you can use .strip() .

To print the special characters, you can by .encode('utf-8') .

To put everything in lower case , put a .lower() at the end of the whole sentence.

To change the various spaces in the middle of the string by a single _ , you can make a .split() to separate all words within the string and then merge them with "_".join() .

Putting the four together would look like this:

print "_".join(celula.encode('utf-8').strip().lower().split())

Since celula is an iteration of each element of linha .

Furthermore, you have a logic error in this section:

yield plan.row_values(0)

Where instead of 0 should be i .

Putting it all back in your code would look like this:

import xlrd


def xlread(arq_xls):

    xls = xlrd.open_workbook(arq_xls)
    # Pega a primeira planilha do arquivo
    plan = xls.sheets()[0]

    # Para i de zero ao numero de linhas da planilha
    for i in xrange(plan.nrows):
        # Le os valores nas linhas da planilha
        yield plan.row_values(i)

for linha in xlread("teste.xlsx"):
    for celula in linha:
        print "_".join(celula.encode('utf-8').strip().lower().split())
    
18.11.2015 / 16:16
0

I believe that spaces can be easily removed as follows:

for linha in xlread("teste.xls"):
    for item in linha:
        print item.strip()

As for the special characters I think you should leave this way, when they are read from the list they should return to their original format.

    
18.11.2015 / 16:11