【问题标题】:Python IndexError : list index out of range PushBulletPython IndexError:列表索引超出范围 PushBullet
【发布时间】:2014-09-21 10:02:12
【问题描述】:

我有问题,我需要帮助。我想在 python 中使用PushBullet,但出现错误。

这是我的代码:

from pushbullet import PushBullet
from pushbullet import device

apik="myapikey"
pb=PushBullet(apik)
de=pb.devices[0]
success, push = de.push_note("adsadasd","asdasdasd asd asd")

我收到此错误:

Traceback (most recent call last):
  File "gggg.py", line 6, in <module>
    de = pb.devices[0]
IndexError: list index out of range

我该如何解决这个问题?

【问题讨论】:

    标签: python list pushbullet


    【解决方案1】:

    表示您没有注册设备。

    使用for循环,可以推送多个设备,即使没有设备也不会抛出异常。

    可能的原因是指定了错误的 api 密钥。

    from pushbullet import PushBullet
    from pushbullet import device
    
    apik = "myapikey"
    pb = PushBullet(apik)
    for de in pb.devices:
        success, push = de.push_note("adsadasd","asdasdasd asd asd")
    

    如果您只想推送到第一个设备,请确保至少有一个设备:

    ...
    pb = PushBullet(apik)
    if pb.devices:
        db = pb.devices[0]:
        success, push = de.push_note("adsadasd","asdasdasd asd asd")
    

    【讨论】:

    • 谢谢您,我现在尝试了您的代码。我没有收到错误但它不起作用:(它没有响应。抱歉英语不好。
    • @BerkayEraydin,别担心英语。检查您是否使用了正确的 api 密钥。
    • @BerkayEraydin,也尝试打印pb.devicespb = PushBullet(apik); print(pb.devices)
    • 啊,是的,我写错了api密钥。我再次复制并粘贴api密钥并解决了。谢谢你。 :)
    • @BerkayEraydin,感谢您的反馈。我相应地更新了答案。
    猜你喜欢
    • 2016-08-25
    • 2017-04-05
    • 2012-07-15
    • 2013-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多