DrWhiter 发表于 2021-6-22 13:56:31

关于ast.literal_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)
报错如下:
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>
我该怎么办?

逃兵 发表于 2021-6-22 14:08:19

报错原因是里面有个字典的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'))

DrWhiter 发表于 2021-6-22 14:44:35

逃兵 发表于 2021-6-22 14:08
报错原因是里面有个字典的value值为false
"hide_click":false



谢谢!
不过我发现我这个版本(3.9.5)的eval()也不能处理false数据,报错如下:Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
    b = eval(a)
File "<string>", line 1, in <module>
NameError: name 'false' is not defined

我一开始的解决方法是直接加入两行代码:
global false, null, true
false = null = true = ''
不过由于eval()函数有安全漏洞,所以我仍然用ast.literal_eval()来互相转化

逃兵 发表于 2021-6-22 14:48:34

DrWhiter 发表于 2021-6-22 14:44
谢谢!
不过我发现我这个版本(3.9.5)的eval()也不能处理false数据,报错如下:
我一开始的解决方法是 ...

在python中
false属于变量
False才是关键字,所以不管哪个版本fasle都会报错
这里是由于false没有定义引发的错误
你在前面声明变量false = null = true = ''
使得false被定义才不会报错
页: [1]
查看完整版本: 关于ast.literal_eval()的问题