Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 嵌套类型的递归字符串到int转换器_Python_String_Integer_Nested_Type Conversion - Fatal编程技术网

Python 嵌套类型的递归字符串到int转换器

Python 嵌套类型的递归字符串到int转换器,python,string,integer,nested,type-conversion,Python,String,Integer,Nested,Type Conversion,反序列化模块有时无法识别值的类型。我想在任何嵌套结构中查找或生成通用转换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: possi

反序列化模块有时无法识别值的类型。我想在任何嵌套结构中查找或生成通用转换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 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]}

如果您有太多的代码要在这里发布,那么就创建一个可以在这里发布的问题的最小版本。如果你不能做到这一点,那么你可能需要考虑一下你的问题,然后把它分成几个小问题。我正要说的和厕所一样。另外,你能问一些关于你的问题的具体问题吗?现在的问题似乎是如何在任何结构中递归地将字符串转换为int。您编写的示例看起来甚至可以修改dict中的键?奇怪。这个过程很琐碎,我在libs中找不到有效的解决方案。另外,如果没有解决方案,我想找到最紧凑的解决方案。@lilo.panic:我不理解您将数字分类为十进制和浮点的逻辑。你打算在这里干什么?为什么要确定小数点后2位的阈值来区分浮点和小数?此外,与当前的检查方式不同,最好使用isinstancespam、collections.iterable。此逻辑是项目的自定义逻辑:2个符号表示成本字段。
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]}