Reading numeric Excel data as text using xlrd in Python -


i trying read in excel file using xlrd, , wondering if there way ignore cell formatting used in excel file, , import data text?

here code using far:

import xlrd  xls_file = 'xltest.xls' xls_workbook = xlrd.open_workbook(xls_file) xls_sheet = xls_workbook.sheet_by_index(0)  raw_data = [['']*xls_sheet.ncols _ in range(xls_sheet.nrows)] raw_str = '' feild_delim = ',' text_delim = '"'  rnum in range(xls_sheet.nrows):     cnum in range(xls_sheet.ncols):         raw_data[rnum][cnum] = str(xls_sheet.cell(rnum,cnum).value)  rnum in range(len(raw_data)):     cnum in range(len(raw_data[rnum])):         if (cnum == len(raw_data[rnum]) - 1):             feild_delim = '\n'         else:             feild_delim = ','         raw_str += text_delim + raw_data[rnum][cnum] + text_delim + feild_delim  final_csv = open('final.csv', 'w') final_csv.write(raw_str) final_csv.close() 

this code functional, there fields, such zip code, imported numbers, have decimal 0 suffix. example, there zip code of '79854' in excel file, imported '79854.0'.

i have tried finding solution in xlrd spec, unsuccessful.

that's because integer values in excel imported floats in python. thus, sheet.cell(r,c).value returns float. try converting values integers first make sure values integers in excel begin with:

cell = sheet.cell(r,c) cell_value = cell.value if cell.ctype in (2,3) , int(cell_value) == cell_value:     cell_value = int(cell_value) 

it is in xlrd spec.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -