Python中,用eval强制将字符串转换为字典变量时候出错:NameError name ‘null‘ is not defined_python将字符串解析为字典,但是布尔值报错(1)

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

---------------------------------搬家声明--------------------------------------

本博客已搬家至个人网站 在路上 - On the way 下面的 技术 分类。

你可以通过点击 更新帖子 【已解决】Python中,用eval强制将字符串转换为字典变量时候出错:NameError: name ‘null’ is not defined 找到当前帖子的新地址。

----------------------------------搬家声明--------------------------------------

【已解决】Python中,用eval强制将字符串转换为字典变量时候出错:NameError: name ‘null’ is not defined

【背景】

在python中,对于一个已经存在的字符串:

{ “reply_count”:3, …, “title”:“”, … }

其内容的形式很类似于字典类型的变量,所以,希望去将其强制转换为字典类型变量,以便于后续处理。

后来找到了介绍,说是用eval强制转换即可。然后去试了,用了eval去转换,发现的确是可以的。

但是,后来去用eval强制转换另外别的一个字符串:

{ “reply_count”:0, …, “title”:null, … }

却出现错误:

Traceback (most recent call last):   File “D:\tmp\WordPress\Others\to_wp\hi-baidu-mover_v2\hi-baidu-mover_v2.py”, line 446, in     main()   File “D:\tmp\WordPress\Others\to_wp\hi-baidu-mover_v2\hi-baidu-mover_v2.py”, line 384, in main     i=fetchEntry(permalink,datetimepattern,mode)   File “D:\tmp\WordPress\Others\to_wp\hi-baidu-mover_v2\hi-baidu-mover_v2.py”, line 98, in fetchEntry     cmt_err_msg = eval(cmt_soup_uni_removeHtmlTag)   File “”, line 1, in NameError: name ‘null’ is not defined

【解决过程】

  1. 网上找了半天,只是看到很多解释,说是最好有节制的使用,尽量少用eval函数,其好像存在一些安全隐患。

推荐使用ast模块。但是ast是python 2.6中才有,而我装得正好只是python 2.5,暂时不方便重新安装升级为2.6,所以继续折腾,看看eval转换为何会出现这个错误。

2.后来通过google搜索到这个:

Running JSON through Python’s eval()? - Stack Overflow

“So, as I’m working with Python 2.4 (i.e. no json module), eval() is  out by SilentGhost: eval doesn’t handle true -> True, false -> False, null -> None correctly.  line 1, in  NameError: name ‘false’ is not defined ”

其中,注意到其解释说,eval不支持null,true,false等,没法正确转换为None,True,False等,所以,才明白,此处的错误,是由于上面的字符串中,包含了“title”:null,eval函数无法正确解析,所以报错“NameError: name ‘null’ is not defined”。

知道了这个原因后,那就明白了,没法继续再用eval了,只有想办法升级python到2.6+的版本了,然后用

ast.literal_eval

去取代eval,即可。

3。刚又找到官方的解释:

http://docs.python.org/library/functions.html#eval

eval(expression[, globals[, locals]])?

The arguments are a string and optional globals and locals. If provided,globals must be a dictionary. If provided, locals can be any mapping object.
Changed in version 2.4: formerly locals was required to be a dictionary.
The expression argument is parsed and evaluated as a Python expression (technically speaking, a condition list) using the globals and localsdictionaries as global and local namespace. If the globals dictionary is present and lacks ‘__builtins__’, the current globals are copied into globalsbefore expression is parsed. This means that expressionnormally has full access to the standard __builtin__ module and restricted environments are propagated. If the locals dictionary is omitted it defaults to theglobalsdictionary. If both dictionaries are omitted, the expression is executed in the environment where eval() is called. The return value is the result of the evaluated expression. Syntax errors are reported as exceptions. Example:

>>> x = 1 >>> print eval('x+1') 2 

This function can also be used to execute arbitrary code objects (such as those created by compile()). In this case pass a code object instead of a string. If the code object has been compiled with ‘exec’ as themode argument, eval()‘s return value will be None.
Hints: dynamic execution of statements is supported by the execstatement. Execution of statements from a file is supported by theexecfile() function. The globals() andlocals() functions returns the current global and local dictionary, respectively, which may be useful to pass around for use by eval() or execfile().
Seeast.literal_eval()for a function that can safely evaluate strings with expressions containing only literals.
|

最后

不知道你们用的什么环境,我一般都是用的Python3.6环境和pycharm解释器,没有软件,或者没有资料,没人解答问题,都可以免费领取(包括今天的代码),过几天我还会做个视频教程出来,有需要也可以领取~

给大家准备的学习资料包括但不限于:

Python 环境、pycharm编辑器/永久激活/翻译插件

python 零基础视频教程

Python 界面开发实战教程

Python 爬虫实战教程

Python 数据分析实战教程

python 游戏开发实战教程

Python 电子书100本

Python 学习路线规划

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值