|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我试着运行下方的代码:a = '{"code":0,"message":"0","ttl":1,"data":{"list":{"tlist":{"36":{"tid":36,"count":1,"name":"知识"}},"vlist":[{"comment":38,"typeid":122,"play":4101,"pic":"5555","subtitle":"","description":"5555","copyright":"1","title":"555","review":0,"author":"光点科技官方","mid":24892260,"created":1612066096,"length":"00:14","video_review":0,"aid":801433227,"bvid":"BV1Ay4y1n73d","hide_click":false,"is_pay":0,"is_union_video":0,"is_steins_gate":0,"is_live_playback":0}]},"page":{"pn":1,"ps":30,"count":1}}}'
import ast
b = ast.literal_eval(a)
报错如下:Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
b = ast.literal_eval(a)
File "D:\Python\lib\ast.py", line 105, in literal_eval
return _convert(node_or_string)
File "D:\Python\lib\ast.py", line 94, in _convert
return dict(zip(map(_convert, node.keys),
File "D:\Python\lib\ast.py", line 94, in _convert
return dict(zip(map(_convert, node.keys),
File "D:\Python\lib\ast.py", line 94, in _convert
return dict(zip(map(_convert, node.keys),
File "D:\Python\lib\ast.py", line 85, in _convert
return list(map(_convert, node.elts))
File "D:\Python\lib\ast.py", line 94, in _convert
return dict(zip(map(_convert, node.keys),
File "D:\Python\lib\ast.py", line 104, in _convert
return _convert_signed_num(node)
File "D:\Python\lib\ast.py", line 78, in _convert_signed_num
return _convert_num(node)
File "D:\Python\lib\ast.py", line 69, in _convert_num
_raise_malformed_node(node)
File "D:\Python\lib\ast.py", line 66, in _raise_malformed_node
raise ValueError(f'malformed node or string: {node!r}')
ValueError: malformed node or string: <ast.Name object at 0x0000018DD0FF1F40>
我该怎么办?
报错原因是里面有个字典的value值为false
"hide_click":false
要么修改为'false',要么修改为False
单纯的false没有定义,引发的错误
另外可以直接用eval函数
a = '{"code":0,"message":"0","ttl":1,"data":{"list":{"tlist":{"36":{"tid":36,"count":1,"name":"知识"}},"vlist":[{"comment":38,"typeid":122,"play":4101,"pic":"5555","subtitle":"","description":"5555","copyright":"1","title":"555","review":0,"author":"光点科技官方","mid":24892260,"created":1612066096,"length":"00:14","video_review":0,"aid":801433227,"bvid":"BV1Ay4y1n73d","hide_click":false,"is_pay":0,"is_union_video":0,"is_steins_gate":0,"is_live_playback":0}]},"page":{"pn":1,"ps":30,"count":1}}}'
import ast
b = ast.literal_eval(a.replace('false','False'))
|
|