From 1674051e653eade02e7af92476a31ff9af0d408f Mon Sep 17 00:00:00 2001 From: Recolic Keghart <root@recolic.net> Date: Wed, 22 Jul 2020 22:57:02 +0800 Subject: [PATCH] bug fix: fucking python num conv error --- xlsx_conv.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/xlsx_conv.py b/xlsx_conv.py index 6d2a0e1..58d366b 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'): -- GitLab