Search code examples
pythonexcelxlrd

Extract xlrd package data extraction


When i am trying to extract the data from an xlsx files. I get the encoding details with the data as well.

Consider the code as shown below,

column_number = 0
column_headers = []
#column_headers = sheet.row_values(row_number)
while column_number <= sheet.ncols - 1:
    column_headers.append(sheet.cell(row_number, column_number).value)
    column_number+=1

return column_headers

output is,

[u'Rec#', u'Cyc#', u'Step', u'TestTime', u'StepTime', u'Amp-hr', u'Watt-hr', u'Amps', u'Volts', u'State', u'ES', u'DPt Time', u'ACR', u'DCIR']

I just want to extract the cell value which is the data without "u'" attached to it . How can i get just that ?


Solution

  • You can use string encoding to convert the unicode to ascii. So your updated code should be

    column_headers.append((sheet.cell(row_number, column_number).value).encode('ascii','ignore'))
    

    You can get the value by using data.value for the content of the field name. Also note that integers are imported as floats by default. So, you may end with with an additional .0 in the end, which you can remove by typecasting the value by using int(data.value).