Python 2.7.9 subprocess convert check_output to dictionary (volumio)

Python 2.7.9 subprocess convert check_output to dictionary (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

胜利!我能够使我的代码与 eval()

一起工作
import subprocess

status = subprocess.check_output("volumio status | sed -e 's/true/True/g' -e 's/false/False/g' -e 's/null/False/g'", shell=True)
status = eval(status)
print status["volume"]

它returns 100