简体   繁体   English

IndexError:列出索引超出范围python json

[英]IndexError: list index out of range python json

When i excecute this code, for a json file当我执行此代码时,对于 json 文件

data = []

with open('subscriberaa') as f:
     for line in f:
        data = json.loads(line)

print str(data["services_key"][0]["code_key"])

I've this error:我有这个错误:

IndexError: list index out of range

This is for a null list into a parse string ([])这是用于将空列表转换为解析字符串 ([])

"services_key" : [] “服务密钥”:[]

How i can try and catch this error?我如何尝试捕获此错误?

in python the term is try and except but i know what you mean在 python 中,这个词是 try and except 但我知道你的意思

here's what you can do这是你可以做的

data = []

with open('subscriberaa') as f:
     for line in f:
        data = json.loads(line)
try:
   print str(data["services_key"][0]["code_key"])
except:
   //code that runs when error happens

hope this helps!希望这可以帮助!

you can use time interval ...您可以使用时间间隔...

import time
data = []

with open('subscriberaa') as f:
     for line in f:
        data = json.loads(line)
        time.sleep(2)

print str(data["services_key"][0]["code_key"])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM