报错NameError: name ‘null’ is not defined的解决方法
报错NameError: name ‘null’ is not defined的解决方法
eval()介绍
eval()函数十分强大,官方demo解释为:将字符串str当成有效的表达式来求值并返回计算结果。
它可以把list,tuple,dict和string相互转化。
在接口自动化中经常用到。
比如啊,我们把测试数据写成数组的格式存放于excle表中,当读取出来时就是str格式,此时用eval,就可以把取到的值转换为正常的数组或者字典的格式了。
NameError: name ‘null’ is not defined是怎么出现的
a = "{"a":1,"b":2,"c":null}"
a = eval(a)
print(a)
在转换的字符串中,存在null时,就会出现NameError: name ‘null’ is not defined这个错误。
解决方法
巧用 replace()方法。
将字符串中的null替换掉!
str = "{"a":1,"b":2,"c":null}"
str.replace("null", "123456")
a = eval(str)
print(a)
这样子就能够将字符串中的null替换掉了。就能够正常的转换了。
应用场景
我为什么要这么做呢?
因为我在做自动化测试的时候,需要在数据库中取出一个[{"a":1,"b":2,"c":null},{"a":1,"b":2,"c":null},{"a":1,"b":2,"c":null}]
这样子格式的数据来和预期值做对比。
我的预期值只是a,所以我要在这个数据中,把a取出来。
所以就需要上述的这种操作了!
a = "[{"a":1,"b":2,"c":null},{"a":1,"b":2,"c":null},{"a":1,"b":2,"c":null}]"
hlist = eval(a)
在这一步的时候因为有null存在,所以报错。
a = "[{"a":1,"b":2,"c":null},{"a":1,"b":2,"c":null},{"a":1,"b":2,"c":null}]"
a = a.replace("null", "123456")
hlist = eval(a)
testhope = 1
if testhope == hlist[0]["a"]:
pass
这样子就可以了,因为null被替换为了123456.
报错NameError: name ‘null’ is not defined的解决方法的更多相关文章
- MongoDB解压报错gzip: stdin: not in gzip format的解决方法
MongoDB解压报错gzip: stdin: not in gzip format的解决方法 在安装MongoDB时出现如下报错: [root@vm172--- mongodb]# tar -zxv ...
- mysql用查询结果当删除的判断条件进行删除报错1093 You can't specify target table解决方法
mysql用查询结果当删除的判断条件进行删除报错1093 You can't specify target table解决方法 #分开两个sql执行正常的语句,只保留最新1000条数据,删掉1000条 ...
- py+selenium 报错NameError: name 'NoSuchElementException' is not defined【已解决】
报错:NameError: name 'NoSuchElementException' is not defined 如图 解决方法: 头部加一句:from selenium.common.exc ...
- Python,报错NameError: name 'math' is not defined
1 #-*- coding : utf-8 -*- 2 import math 3 4 def move(x, y, step, angle=0): 5 nx = x + step * math.co ...
- python2执行程序报错:NameError: name 'y' is not defined
然后运行一直报错 这个错误,是由于版本不同造成的语法错误,在python3中,输入可以使用input,但如果你的版本是python2,那么此时input就得改成raw_input,否则你输入数据会被当 ...
- python中引入包的时候报错AttributeError: module 'sys' has no attribute 'setdefaultencoding'解决方法?
python中引入包的时候报错:import unittestimport smtplibimport timeimport osimport sysimp.reload(sys)sys.setdef ...
- 修改ubuntu DNS的步骤/wget url报错: unable to resolve host address的解决方法
wget url 报错:unable to resolve host address ‘url’,显然是无法解析主机地址,这就能看出是DNS解析的问题.解决办法就是配置可用的dns 一般是修改成为谷歌 ...
- loadruner报错:Step download timeout(120 seconds)的一个解决方法
一个网友问了我一个问题如下: loadruner报错:Error -27728: Step download timeout (120 seconds) 如何解决 语 法检查通过,但是在并发执行一个查 ...
- Centos6 下启动httpd报错 Could not reliably determine the server's解决方法
在启动httpd的时候报错: 修改/etc/httpd/conf/httpd.conf 配置,去掉ServerName 前的#(或者手动添加ServerName localhost:80)然后重启ht ...
随机推荐
- 【原创】kubernetes之CNI理解
一.什么是CNI? CNI-容器网络接口,CNI(容器网络接口)是Cloud Native Computing Foundation项目,由一个规范和库(用于编写用于在Linux容器中配置网络接口的插 ...
- VMware虚拟化与Kubernetes(K8s)类比阐述-适合VMware用户
概述 容器技术是最近几年非常热门的技术,它似乎就是为云端的应用量身定制的,所以它也被贴上了云原生应用 (Cloud Native Application) 技术的标签.目前最为流行的容器管理调度平台是 ...
- Redis 主从复制(Replication)
为了保证服务的可用性,现代数据库都提供了复制功能,同时在多个进程中维护一致的数据状态. Redis 支持一主多从的复制架构,该功能被简化成了一条 SLAVEOF 命令,下面通过条命令来解析 Redis ...
- POJ 2923 Relocation(状压DP)题解
题意:有2辆车运货,每次同时出发,n(<10),各自装货容量c1 c2,问最少运几次运完. 思路:n比较小,打表打出所有能运的组合方式,用背包求出是否能一次运走.然后状压DP运的顺序. 代码: ...
- Gym 101170I Iron and Coal(BFS + 思维)题解
题意:有一个有向图,有些点是煤,有些点是铁,但不会同时有铁和煤.现在我要从1出发,占领可以到达的点.问最少占领几个点能同时拥有一个煤和一个铁(1不用占领). 思路:思路很秀啊.我们从1往外bfs,得到 ...
- 关于st表的推导
#include <bits/stdc++.h> using namespace std; const int maxn=1e6+7; int st[maxn][32]; int a[ma ...
- Ubuntu16.04+wineQQ+解决版本过低
[参考1:] http://blog.csdn.net/sinat_32079337/article/details/72771078? [参考2:] http://blog.csdn.net/qq_ ...
- MySQL 8.x
MySQL 8.x SQL & NoSQL $ mysql --version # mysql Ver 8.0.21 for osx10.15 on x86_64 (Homebrew) # M ...
- text to JSON
text to JSON GeoLocaltion API https://www.cnblogs.com/xgqfrms/p/13283680.html https://repl.it/@xgqfr ...
- vue & child component & props
vue & child component & props vue pass data to child component https://vuejs.org/v2/guide/co ...