0

解串行模块有时无法识别值的类型。我想在任何嵌套结构中找到或创建通用转换 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]}
4

0 回答 0