Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.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将字符串转换为字典(格式错误的字符串)_Python_Python 2.7 - Fatal编程技术网

如何使用python将字符串转换为字典(格式错误的字符串)

如何使用python将字符串转换为字典(格式错误的字符串),python,python-2.7,Python,Python 2.7,我试过两件事 使用ast.literal_eval(a)时,输出的字符串格式不正确 在使用json.loads(a)或simplejson.loads(a)时。。产量仍在增长 串 我该怎么做。。如果字符串看起来像“{Po_list:[121122],product_list:[34343434],site_list:[2,63]}”,要获得作为字典的输出,您只需在计算a之前通过在其周围添加引号将键转换为字符串: a = {Po_list:[121,122],product_list:[343,4

我试过两件事

  • 使用ast.literal_eval(a)时,输出的字符串格式不正确

  • 在使用json.loads(a)或simplejson.loads(a)时。。产量仍在增长


  • 我该怎么做。。如果字符串看起来像
    “{Po_list:[121122],product_list:[34343434],site_list:[2,63]}”
    ,要获得作为字典的输出,您只需在计算
    a
    之前通过在其周围添加引号将键转换为字符串:

    a = {Po_list:[121,122],product_list:[343,434],site_list:[2,63]}
    
    type(a) is 'str'
    
    >>重新导入
    >>>从ast导入文字值
    >>>a=“{Po_列表:[121122],产品_列表:[343434],站点_列表:[2,63]}”
    >>>re.sub(“(\w+(=:)”,“\g\”,a)
    “{“采购清单”:[121122],“产品清单”:[343434],“现场清单”:[2,63]}”
    >>>文字评估(re.sub(“(\w+(=:)”,“\g\”,a))
    {'Po_list':[121122],'product_list':[343434434],'site_list':[263]}
    
    如果您的字符串看起来像
    “{Po_list:[121122],product_list:[343434],site_list:[2,63]}”
    ,则在计算
    a
    之前,您只需通过在其周围添加引号将键转换为字符串:

    a = {Po_list:[121,122],product_list:[343,434],site_list:[2,63]}
    
    type(a) is 'str'
    
    >>重新导入
    >>>从ast导入文字值
    >>>a=“{Po_列表:[121122],产品_列表:[343434],站点_列表:[2,63]}”
    >>>re.sub(“(\w+(=:)”,“\g\”,a)
    “{“采购清单”:[121122],“产品清单”:[343434],“现场清单”:[2,63]}”
    >>>文字评估(re.sub(“(\w+(=:)”,“\g\”,a))
    {'Po_list':[121122],'product_list':[343434434],'site_list':[263]}
    
    文字评估
    有效。你不知怎么给它输入了错误的信息

    >>> import re
    >>> from ast import literal_eval
    >>> a = "{Po_list:[121,122],product_list:[343,434],site_list:[2,63]}"
    >>> re.sub('(\w+)(?=:)', '"\g<1>\"', a)
    '{"Po_list":[121,122],"product_list":[343,434],"site_list":[2,63]}'
    >>> literal_eval(re.sub('(\w+)(?=:)', '"\g<1>\"', a))
    {'Po_list': [121, 122], 'product_list': [343, 434], 'site_list': [2, 63]}
    
    a='{“采购清单”:[121122],“产品清单”:[34343434],“现场清单”:[2,63]}'
    打印(类型(a))#打印:
    从ast导入文字评估为leval
    res=左旋(a)
    打印(打字(res))#
    

    如果我理解错误,并且没有引用密钥,您可能应该使用
    regex
    来修复这一问题;请看@EricDuminil的答案。你不知怎么给它输入了错误的信息

    >>> import re
    >>> from ast import literal_eval
    >>> a = "{Po_list:[121,122],product_list:[343,434],site_list:[2,63]}"
    >>> re.sub('(\w+)(?=:)', '"\g<1>\"', a)
    '{"Po_list":[121,122],"product_list":[343,434],"site_list":[2,63]}'
    >>> literal_eval(re.sub('(\w+)(?=:)', '"\g<1>\"', a))
    {'Po_list': [121, 122], 'product_list': [343, 434], 'site_list': [2, 63]}
    
    a='{“采购清单”:[121122],“产品清单”:[34343434],“现场清单”:[2,63]}'
    打印(类型(a))#打印:
    从ast导入文字评估为leval
    res=左旋(a)
    打印(打字(res))#
    

    如果我理解错误,并且没有引用密钥,您可能应该使用
    regex
    来修复这一问题;看看@EricDuminil的答案,到底应该是什么?如果
    a
    在其定义中是一个字符串,请将其置于引号中。钥匙也是如此。把你的文本移到代码块之外,到底应该怎么写?如果
    a
    是定义上的字符串,就把它放在引号里。钥匙也是如此。并将文本移到代码块之外