python报错:ValueError: dictionary update sequence element #1 has length 6; 2 is required解决

    xiaoxiao2023-11-27  183

    一、问题描述,直接使用dict方法转换字符串为字典

    >>> a = '{"sdf":"sdf"}' >>> dict(a) Traceback (most recent call last): File "<input>", line 1, in <module> ValueError: dictionary update sequence element #0 has length 1; 2 is required >>> a = '{"sd":"ed"}' >>> dict(a) Traceback (most recent call last): File "<input>", line 1, in <module> ValueError: dictionary update sequence element #0 has length 1; 2 is required

    二、问题解决 使用eval

    >>> a = '{"sdf":"sdf"}' >>> eval(a) {'sdf': 'sdf'}

    原文链接地址: https://blog.csdn.net/weixin_40894428/article/details/80683137 字符串转字典要用eval(),这个方法很多书上都没有介绍, eval转换字符串、列表、字典: https://www.cnblogs.com/nancyzhu/p/8119862.html

    最新回复(0)