解析json文件时出现关键错误。
json文件 config.json:
{
"user": {
"name": "hsani",
"password": "AQ@SWkeet999"
},
"env": {
"Staging": "https://serviceconsole-stg.tivo.com/serviceconsole/login.action",
"QE": "http://serviceconsolese01.tivo.com:9090/serviceconsole"
}
}
我在这里称呼它
with open('config.json', 'r') as login_details:
login_conf = json.load(login_details)
#print config["Staging"]
if env == 'Staging':
self.driver.get(login_conf['env']['Staging'])
elif env == 'QE':
self.driver.get(login_conf['env']['QE'])
获得的错误是
Traceback (most recent call last):
File "testdemo.py", line 9, in <module>
driver = MSOSOLUTION.login.BrowserLogin('Staging')
File "../lib/python/MSOSOLUTION/login.py", line 20, in __init__
self.driver.get(login_conf['env']['Staging'])
KeyError: 'env
有人可以解释如何解决关键错误吗?
答案 0 :(得分:1)
一些注意事项:Vidhya
,
和user
之间缺少env
login_conf.get('env').get('qe', None)
QE
而不是qe
答案 1 :(得分:0)
您在"user":{}
之后得到一个逗号
还要更改
with open('data.json') as data_file:
data = json.loads(data_file.read())
使用json.loads
代替json.load
。在Python 3中,json.loads
采用字符串参数。 json.load
采用类似文件的对象参数。 data_file.read()
返回一个字符串对象。
{
"user": {
"name": "hsani",
"password": "AQ@SWkeet999"
},
"env": {
"Staging": "https://serviceconsole-stg.tivo.com/serviceconsole/login.action",
"QE": "http://serviceconsolese01.tivo.com:9090/serviceconsole"
}
}