字符串转字典报错:ValueError: dictionary update sequence element #0 has length 1; 2 is required

字符串转字典

data = opener.open(url)
data=data.read().decode('utf-8')
data=dict(data)

报错如下

ValueError: dictionary update sequence element #0 has length 1; 2 is required

字符串转字典报错:ValueError: dictionary update sequence element #0 has length 1; 2 is required_第1张图片

解决方法

使用python 内置函数eval将字符串转为字典

eval(expression[, globals[, locals]])
data = opener.open(url)
data=data.read().decode('utf-8')
data=eval(data)
  • 执行成功
    字符串转字典报错:ValueError: dictionary update sequence element #0 has length 1; 2 is required_第2张图片

你可能感兴趣的:(#,python错误收集)