Search code examples
pythonintxlrd

Python xlrd saving floats instead of ints while convertir xlsx to csv


I'm using xlrdto read a .xlsx file and save them to a .csv file. Everything is ok, the problem is that all the int values of the .xlsx file are converted to float automatically on the .csv file. This means that if I've a 40 inside of a cell of the .xlsxfile, it appears as 40.0on the .csv file.

I use the following code to read and convert it to .csv.

    wb = xlrd.open_workbook('share\docs\excelcontrol2.xlsx')
    sh = wb.sheet_by_name('Hoja1')
    archivo_csv = open('share\docs\output.csv', 'wb')
    wr = csv.writer(archivo_csv, delimiter=";")
    for rownum in xrange(sh.nrows):
        wr.writerow(sh.row_values(rownum))
    archivo_csv.close()

The .xlsx files contains int and float among other stuff. How can I save the .csv file to keep the original format? I mean, whitout changing the int to float and leave the rest as it is?

Thanks in advance.


Solution

  • According to xlrd docs the Excel XL_CELL_NUMBER will be converted to Python float type.

    I think this is the reason that your int values are converted to floats.