Python 2.7.9子进程将check_output转换为字典(volumio)

时间:2018-01-07 01:17:14

标签: python-2.7 dictionary raspberry-pi subprocess volumio

我一直在寻找,但我找不到答案。 我正在我的Raspberry Pi上为volumio制作一个脚本

在终端中,当我输入

volumio status

我完全

{
  "status": "pause",
  "position": 0,
  "title": "Boom Boom",
  "artist": "France Gall",
  "album": "Francegall Longbox",
  "albumart": "/albumart?cacheid=614&web=France%20Gall/Francegall%20Longbox/extralarge&path=%2FUSB&metadata=false",
  "uri": "/Boom Boom.flac",
  "trackType": "flac",
  "seek": 21192,
  "duration": 138,
  "samplerate": "44.1 KHz",
  "bitdepth": "16 bit",
  "channels": 2,
  "random": true,
  "repeat": null,
  "repeatSingle": false,
  "consume": false,
  "volume": 100,
  "mute": false,
  "stream": "flac",
  "updatedb": false,
  "volatile": false,
  "service": "mpd"
}

在python中,我想将它存储在字典中

因为它已经有了正确的格式,我认为将它分配给变量会立即使它成为一个字典,如下所示:

import subprocess, shlex
cmd = "volumio status | sed -e 's/true/True/g' -e 's/false/False/g' -e 's/null/False/g'"
cmd = shlex.split(cmd)
status = subprocess.check_output(cmd)

print status["volume"]

如果我认为是真的,我会得到“100”。相反,我得到了这个错误:

File "return.py", line 7, in <module>
    print status["volume"]
TypeError: string indices must be integers, not str

这意味着“status”存储为字符串。有谁知道我怎么能把它变成字典?

dict()没有成功,我得到:

ValueError: dictionary update sequence element #0 has length 1; 2 is required

1 个答案:

答案 0 :(得分:0)

胜利!我能够使用eval()

使我的代码工作
SwingUtilities.updateComponentTreeUI(frame);

它返回100