我正在编写一个脚本来从管道中读取一些数据并解析参数。我有一个这样的脚本:
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>
任何帮助将不胜感激。谢谢。
你混淆了==
和=
。您没有向字典添加任何内容,而是尝试将某些内容与尚不存在的元素进行比较。ssnc_params[decoded_data[1]] == decoded_data[2:]
必须是ssnc_params[decoded_data[1]] = decoded_data[2:]
。与core_params
.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句