尝试添加到 dict 会引发 KeyError - Python3.5.3

幸运的

我正在编写一个脚本来从管道中读取一些数据并解析参数。我有一个这样的脚本:

def metadataScrape():
    current_line = ""
    line_finished = False
    with open("/tmp/shairport-sync-metadata") as f:
        for line in f:
            current_line += line[:-1]
            if current_line[-2:] == "m>":
                line_finished = True

            if line_finished:
                html_data = html.fromstring(current_line)
                data = html_data.xpath("//text()")
                decoded_data = [HexDecode(data[0]), HexDecode(data[1])]
                if len(data) == 4:
                    decoded_data.append(Base64Decode(data[3]))
                else:
                    decoded_data.append("")
                decoded_data.append(time.time())
                print(decoded_data)
                if decoded_data[0] == "ssnc":
                    ssnc_params[decoded_data[1]] == decoded_data[2:]
                elif decoded_data[0] == "core":
                    core_params[decoded_data[1]] == decoded_data[2:]

                current_line = ""
                line_finished = False

每当我运行它时,它似乎运行良好,直到我获得一些数据。例如,如果我尝试更改 AirPlay 机器上的音量(我的项目是 AirPlay 扬声器),它会在我尝试将其添加到字典时抛出:

Traceback (most recent call last):
  File "<pyshell#42>", line 1, in <module>
    metadataScrape()
  File "/home/pi/python/metadata-reader.py", line 57, in metadataScrape
    ssnc_params[decoded_data[1]] == decoded_data[2:]
KeyError: 'pvol'

仅供参考,这是我从管道中得到的那种东西的样本:

<item><type>636f7265</type><code>6173676e</code><length>3</length>
<data encoding="base64">
UG9w</data></item>
<item><type>636f7265</type><code>6d696e6d</code><length>27</length>
<data encoding="base64">
Q3JhenkgaW4gTG92ZSAoZmVhdC4gSmF5LVop</data></item>
<item><type>636f7265</type><code>6173646b</code><length>1</length>
<data encoding="base64">
AA==</data></item>
<item><type>636f7265</type><code>63617073</code><length>1</length>
<data encoding="base64">
Ag==</data></item>
<item><type>73736e63</type><code>6d64656e</code><length>10</length>
<data encoding="base64">
MTE1NDkyNzQwMg==</data></item>
<item><type>73736e63</type><code>7072736d</code><length>0</length></item>
<item><type>73736e63</type><code>70656e64</code><length>0</length></item>

任何帮助将不胜感激。谢谢。

DYZ

你混淆了===您没有向字典添加任何内容,而是尝试将某些内容与尚不存在的元素进行比较。ssnc_params[decoded_data[1]] == decoded_data[2:]必须是ssnc_params[decoded_data[1]] = decoded_data[2:]core_params.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Python dict.get()引发KeyError

来自分类Dev

为什么 dict.popitem() 会引发 KeyError 而不是返回 None?

来自分类Dev

尝试将dict写入文件时出现KeyError [Python]

来自分类Dev

作为Cronjob运行时,Python3:configparser KeyError

来自分类Dev

Python:将dict值追加到列表(如果已存在)会添加到所有键的值,而不是

来自分类Dev

添加更多字典时python中的KeyError 3

来自分类Dev

python2和python3中dict的__repr __()

来自分类Dev

正在获取KeyError:3

来自分类Dev

尝试将标签添加到d3,无法弄清楚文本标签

来自分类Dev

Python3:dict构造函数是否创建深层副本?

来自分类Dev

TypeError:'dict'对象不可调用python3

来自分类Dev

将dicts列表转换为dicts python3的dict

来自分类Dev

Python3 | 将装饰器添加到枚举项

来自分类Dev

需要帮助添加到python3中的JSON字典

来自分类Dev

Python3:遍历对象并将属性添加到数组或对象

来自分类Dev

如何将mac添加到python3中的列表中?

来自分类Dev

Python3 - 将用户响应添加到列表而不是覆盖现有数据

来自分类Dev

Python3如何将字符串添加到列表中的整数

来自分类Dev

将存储在变量中的字典值添加到列表 python3

来自分类Dev

更新字典中的列表添加到每个键 Python3 中的列表

来自分类Dev

将日历插件添加到MVC 5 / Bootstrap 3中,不确定如何导入文件

来自分类Dev

将日历插件添加到MVC 5 / Bootstrap 3中,不确定如何导入文件

来自分类Dev

为什么在访问未设置的属性时Python会引发KeyError?

来自分类Dev

Python将元素添加到dict和defaultdict

来自分类Dev

将python dict值添加到自身

来自分类Dev

Python将先前的dict值添加到当前值

来自分类Dev

python KeyError:'sapi5'

来自分类Dev

为什么dict()中的key比Python3中的dict.get(key)更快?

来自分类Dev

python3和psycopg2,检查dict_valus和dict_keys

Related 相关文章

  1. 1

    Python dict.get()引发KeyError

  2. 2

    为什么 dict.popitem() 会引发 KeyError 而不是返回 None?

  3. 3

    尝试将dict写入文件时出现KeyError [Python]

  4. 4

    作为Cronjob运行时,Python3:configparser KeyError

  5. 5

    Python:将dict值追加到列表(如果已存在)会添加到所有键的值,而不是

  6. 6

    添加更多字典时python中的KeyError 3

  7. 7

    python2和python3中dict的__repr __()

  8. 8

    正在获取KeyError:3

  9. 9

    尝试将标签添加到d3,无法弄清楚文本标签

  10. 10

    Python3:dict构造函数是否创建深层副本?

  11. 11

    TypeError:'dict'对象不可调用python3

  12. 12

    将dicts列表转换为dicts python3的dict

  13. 13

    Python3 | 将装饰器添加到枚举项

  14. 14

    需要帮助添加到python3中的JSON字典

  15. 15

    Python3:遍历对象并将属性添加到数组或对象

  16. 16

    如何将mac添加到python3中的列表中?

  17. 17

    Python3 - 将用户响应添加到列表而不是覆盖现有数据

  18. 18

    Python3如何将字符串添加到列表中的整数

  19. 19

    将存储在变量中的字典值添加到列表 python3

  20. 20

    更新字典中的列表添加到每个键 Python3 中的列表

  21. 21

    将日历插件添加到MVC 5 / Bootstrap 3中,不确定如何导入文件

  22. 22

    将日历插件添加到MVC 5 / Bootstrap 3中,不确定如何导入文件

  23. 23

    为什么在访问未设置的属性时Python会引发KeyError?

  24. 24

    Python将元素添加到dict和defaultdict

  25. 25

    将python dict值添加到自身

  26. 26

    Python将先前的dict值添加到当前值

  27. 27

    python KeyError:'sapi5'

  28. 28

    为什么dict()中的key比Python3中的dict.get(key)更快?

  29. 29

    python3和psycopg2,检查dict_valus和dict_keys

热门标签

归档