@@ -163,13 +163,14 @@ def convert_field(self, value, conversion):
163
163
spec_regexes ['E' ] = spec_regexes ['f' ]
164
164
spec_regexes ['g' ] = spec_regexes ['f' ]
165
165
spec_regexes ['X' ] = spec_regexes ['x' ]
166
- allow_multiple = ['c' , 'd' , 'o' , 's' , 'x' , 'X' ]
166
+ spec_regexes ['' ] = spec_regexes ['s' ]
167
+ allow_multiple = ['c' , 'd' , 'o' , 's' , '' , 'x' , 'X' ]
167
168
fixed_point_types = ['f' , 'e' , 'E' , 'g' ]
168
169
# format_spec ::= [[fill]align][sign][#][0][width][,][.precision][type]
169
170
# https://docs.python.org/3.4/library/string.html#format-specification-mini-language
170
171
fmt_spec_regex = re .compile (
171
172
r'(?P<align>(?P<fill>.)?[<>=^])?(?P<sign>[\+\-\s])?(?P<pound>#)?(?P<zero>0)?(?P<width>\d+)?'
172
- r'(?P<comma>,)?(?P<precision>.\d+)?(?P<type>[bcdeEfFgGnosxX%])' )
173
+ r'(?P<comma>,)?(?P<precision>.\d+)?(?P<type>[bcdeEfFgGnosxX%]? )' )
173
174
174
175
175
176
def _get_fixed_point_regex (regex_dict , width , precision ):
@@ -294,7 +295,7 @@ def format_spec_to_regex(field_name, format_spec):
294
295
if fill is None :
295
296
if width is not None and width [0 ] == '0' :
296
297
fill = '0'
297
- elif ftype in ['s' , 'd' ]:
298
+ elif ftype in ['s' , '' , ' d' ]:
298
299
fill = ' '
299
300
300
301
char_type = spec_regexes [ftype ]
@@ -304,7 +305,7 @@ def format_spec_to_regex(field_name, format_spec):
304
305
width = width ,
305
306
precision = precision
306
307
)
307
- if ftype == 's' and align and align .endswith ('=' ):
308
+ if ftype in ( 's' , '' ) and align and align .endswith ('=' ):
308
309
raise ValueError ("Invalid format specification: '{}'" .format (format_spec ))
309
310
final_regex = char_type
310
311
if ftype in allow_multiple and (not width or width == '0' ):
@@ -387,19 +388,15 @@ def _get_number_from_fmt(fmt):
387
388
388
389
def _convert (convdef , stri ):
389
390
"""Convert the string *stri* to the given conversion definition *convdef*."""
390
- is_fixed_point = any ([ftype in convdef for ftype in fixed_point_types ])
391
391
if '%' in convdef :
392
392
result = dt .datetime .strptime (stri , convdef )
393
- elif 'd' in convdef or 's' in convdef or is_fixed_point :
394
- stri = _strip_padding (convdef , stri )
395
- if 'd' in convdef :
396
- result = int (stri )
397
- elif is_fixed_point :
398
- result = float (stri )
399
- else :
400
- result = stri
401
393
else :
402
- result = stri
394
+ result = _strip_padding (convdef , stri )
395
+ if 'd' in convdef :
396
+ result = int (result )
397
+ elif any (float_type_marker in convdef for float_type_marker in fixed_point_types ):
398
+ result = float (result )
399
+
403
400
return result
404
401
405
402
0 commit comments