鱼C论坛

 找回密码
 立即注册
查看: 2699|回复: 3

[已解决]关于ast.literal_eval()的问题

[复制链接]
发表于 2021-6-22 13:56:31 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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>
我该怎么办?
最佳答案
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'))
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 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'))
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 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()来互相转化
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 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被定义才不会报错
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-1-15 06:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表