【问题标题】:Python KeyError [closed]Python KeyError [关闭]
【发布时间】:2013-02-05 16:07:50
【问题描述】:

我的这个 python 代码有一个键错误

service = self.get_cd_service()
browse_response = service.Browse(ObjectID=str(object_id),
                                 BrowseFlag=browse_flag,
                                 Filter=filter,
                                 StartingIndex=starting_index,
                                 RequestedCount=requested_count,
                                 SortCriteria=sort_criteria)
elt = Element.from_string(browse_response['Result'])
browse_response['Result'] = elt.get_items()
return browse_response

错误是:

Traceback (most recent call last):
  File "CommandlLineControlPointAv.py", line 93, in <module>
    main()
  File "CommandlLineControlPointAv.py", line 89, in main
    cmdline.run()
  File "CommandlLineControlPointAv.py", line 68, in run
    self.cmd_browse(c.split(' ')[1])
  File "CommandlLineControlPointAv.py", line 48, in cmd_browse
    result = self.browse(id, 'BrowseDirectChildren', '*', 0, 10)['Result']
  File "/usr/local/lib/python2.7/dist-packages/brisa/upnp/control_point/control_point_av.py", line 136, in browse
    elt = Element.from_string(browse_response['Result'])
KeyError: 'Result'

感谢您的帮助/ 最好的问候。

雷姆

【问题讨论】:

  • service.Browse() 可能不会返回字典。
  • 浏览响应是什么样的?关键错误是在你设置elt的那一行,还是后面的那一行?
  • @GrijeshChauhan 如果对象不可下标,您将获得 TypeError,而不是 KeyError
  • @chepner 是的,因为Liststuples 不能被字符串索引。我说的对吗
  • 如果你尝试给一个列表或元组一个字符串索引,你也会得到一个TypeError,而不是KeyErrorbrowse_response 似乎是 dict,只是没有“结果”键。

标签: python python-2.7 upnp


【解决方案1】:

browse_response 没有密钥 Result 检查 browse_reponse 包含的内容

您可以在service.Browse 调用后立即加入调试器 import pdb; pdb.set_trace()

您可以通过 python logging 记录broser_response 对象

【讨论】:

    猜你喜欢
    • 2013-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多