deserial模块有时无法识别值的类型。我想在任何嵌套结构中找到或创建通用转换str-> int的函数。我写了一些东西,但看起来很难看。
转换为一个项目
from decimal import Decimal
def convert_from(s):
if not isinstance(s, basestring):
return s
possible_types = [float, Decimal, int]
if '.' in s:
possible_types.remove(int) # it cannot be!
parts = s.split('.')
if len(parts) > 2:
return s
if len(parts[1]) <= 2:
possible_types.remove(float)
else:
possible_types.remove(Decimal)
possible_types.remove(float)
for convertor in possible_types:
try:
return convertor(s)
except:
pass
return s
快捷方式:
def is_iterable(iterable):
try:
it = iter(iterable)
except:
return False
return True
def process_plain(iterable): # convert a signle list
res = []
for k in iterable:
pk = k
if isinstance(k, basestring):
pk = convert_from(k)
elif is_iterable(k):
pk = convert_all(k)
res.append(pk)
return res
决赛:
def convert_all(iterable):
if isinstance(iterable, basestring) or not is_iterable(iterable):
return iterable
keys = list(iterable)
vals = []
if isinstance(iterable, dict):
vals = iterable.values()
processed_keys = process_plain(keys) #[]
processed_vals = process_plain(vals) #[]
if isinstance(iterable, dict):
return type(iterable)(zip(processed_keys, processed_vals))
return type(iterable)(processed_keys)
结果:
d = {
'a': 5,
'2': ['1', 2, '3'],
666: {
'15': 25,
3: '255',
('55',66,'12', '2.00'): 'string',
'nested again': {
5: '12',
'12.12': 5
}
}
}
print convert_all( d )
{'a': 5, 666: {3: 255, 'nested again': {Decimal('12.12'): 5, 5: 12}, (55, 66, 12, Decimal('2.00')): 'string', 15: 25}, 2: [1, 2, 3]}