|
6 | 6 | :copyright: (c) 2015-2017 by Onni Software Ltd & its contributors
|
7 | 7 | :license: New BSD License
|
8 | 8 | """
|
9 |
| -import re |
10 |
| - |
11 | 9 | import html5lib
|
12 | 10 | import xml.etree.ElementTree as etree
|
13 | 11 |
|
14 |
| -import datetime |
15 | 12 | from pyexcel_io.book import BookReader
|
16 | 13 | from pyexcel_io.sheet import SheetReader, NamedContent
|
17 | 14 | from pyexcel_io._compact import OrderedDict
|
| 15 | +import pyexcel_io.service as service |
18 | 16 |
|
19 | 17 |
|
20 | 18 | ALL_TABLE_COLUMNS = './/*[name()="td" or name()="th"]'
|
@@ -82,17 +80,17 @@ def column_iterator(self, row):
|
82 | 80 | def __convert_cell(self, cell_text):
|
83 | 81 | ret = None
|
84 | 82 | if self.__auto_detect_int:
|
85 |
| - ret = _detect_int_value(cell_text) |
| 83 | + ret = service.detect_int_value(cell_text) |
86 | 84 | if ret is None and self.__auto_detect_float:
|
87 |
| - ret = _detect_float_value(cell_text) |
| 85 | + ret = service.detect_float_value(cell_text) |
88 | 86 | shall_we_ignore_the_conversion = (
|
89 | 87 | (ret in [float('inf'), float('-inf')]) and
|
90 | 88 | self.__ignore_infinity
|
91 | 89 | )
|
92 | 90 | if shall_we_ignore_the_conversion:
|
93 | 91 | ret = None
|
94 | 92 | if ret is None and self.__auto_detect_datetime:
|
95 |
| - ret = _detect_date_value(cell_text) |
| 93 | + ret = service.detect_date_value(cell_text) |
96 | 94 | if ret is None:
|
97 | 95 | ret = cell_text
|
98 | 96 | return ret
|
@@ -145,58 +143,6 @@ def fromstring(s):
|
145 | 143 | return p.parse(s)
|
146 | 144 |
|
147 | 145 |
|
148 |
| -def _detect_date_value(csv_cell_text): |
149 |
| - """ |
150 |
| - Read the date formats that were written by csv.writer |
151 |
| - """ |
152 |
| - ret = None |
153 |
| - try: |
154 |
| - if len(csv_cell_text) == 10: |
155 |
| - ret = datetime.datetime.strptime( |
156 |
| - csv_cell_text, |
157 |
| - "%Y-%m-%d") |
158 |
| - ret = ret.date() |
159 |
| - elif len(csv_cell_text) == 19: |
160 |
| - ret = datetime.datetime.strptime( |
161 |
| - csv_cell_text, |
162 |
| - "%Y-%m-%d %H:%M:%S") |
163 |
| - elif len(csv_cell_text) > 19: |
164 |
| - ret = datetime.datetime.strptime( |
165 |
| - csv_cell_text[0:26], |
166 |
| - "%Y-%m-%d %H:%M:%S.%f") |
167 |
| - except ValueError: |
168 |
| - pass |
169 |
| - return ret |
170 |
| - |
171 |
| - |
172 |
| -def _detect_float_value(csv_cell_text): |
173 |
| - try: |
174 |
| - should_we_skip_it = (csv_cell_text.startswith('0') and |
175 |
| - csv_cell_text.startswith('0.') is False) |
176 |
| - if should_we_skip_it: |
177 |
| - # do not convert if a number starts with 0 |
178 |
| - # e.g. 014325 |
179 |
| - return None |
180 |
| - else: |
181 |
| - return float(csv_cell_text) |
182 |
| - except ValueError: |
183 |
| - return None |
184 |
| - |
185 |
| - |
186 |
| -def _detect_int_value(csv_cell_text): |
187 |
| - if csv_cell_text.startswith('0') and len(csv_cell_text) > 1: |
188 |
| - return None |
189 |
| - try: |
190 |
| - return int(csv_cell_text) |
191 |
| - except ValueError: |
192 |
| - pattern = '([0-9]+,)*[0-9]+$' |
193 |
| - if re.match(pattern, csv_cell_text): |
194 |
| - integer_string = csv_cell_text.replace(',', '') |
195 |
| - return int(integer_string) |
196 |
| - else: |
197 |
| - return None |
198 |
| - |
199 |
| - |
200 | 146 | def text_from_element(elem):
|
201 | 147 | builder = []
|
202 | 148 | for x in elem.iter():
|
|
0 commit comments