diff --git a/xlsx_conv.py b/xlsx_conv.py index 6d2a0e12864c914abf8b4496a47b7602ee46a720..58d366b6df4fef0de3a7db4e2bf2c4c475d25645 100644 --- a/xlsx_conv.py +++ b/xlsx_conv.py @@ -1,6 +1,20 @@ import xlrd import csv, io +def float_to_str(f): + float_string = repr(f) + if 'e' in float_string: # detect scientific notation + digits, exp = float_string.split('e') + digits = digits.replace('.', '').replace('-', '') + exp = int(exp) + zero_padding = '0' * (abs(int(exp)) - 1) # minus 1 for decimal point in the sci notation + sign = '-' if f < 0 else '' + if exp > 0: + float_string = '{}{}{}.0'.format(sign, digits, zero_padding) + else: + float_string = '{}0.{}{}'.format(sign, zero_padding, digits) + return float_string + def xlsx2csv(xlsxPath, sheetIndex, outputFd): wb = xlrd.open_workbook(xlsxPath) sh = wb.sheet_by_index(sheetIndex) @@ -8,7 +22,7 @@ def xlsx2csv(xlsxPath, sheetIndex, outputFd): def fuck_number_ele(ele): if type(ele) is float: - s = str(ele) + s = float_to_str(ele) if 'e' in s.lower(): raise RuntimeError('Fuck python Float2Str: THIS IS A BUG. PLEASE REPORT.') if s.endswith('.0'):