【发布时间】:2017-08-07 14:26:20
【问题描述】:
我正在使用subprocess check_output,并且我有一个输出字符串可以使用:
import subprocess
import sys
command = 'some command'
a = subprocess.check_output(command,shell=True).decode(sys.stdout.encoding)
print(repr(a))
我正在接收这个字符串作为输出。
[ { id: 'id_number1',\n status: 'running'},\n { id: 'id_number2',\n status: 'running'}]\n
以字符串为例,在实际字符串中每个对象有 20+ 个 key ,对象{.. }可能是 1 或 10
1 个对象的真实字符串
[ { id: '6b2708c992d1b469f32c9d1143ed9a758a20ef57',\n status: 'running',\n configPath: 'D:\\\\Sre\\\\',\n uptime: '2h 4m 35s',\n restarts: 0,\n peers: 172,\n offers: 417,\n dataReceivedCount: 6,\n delta: '-12ms',\n port: 4232,\n stor: '25',\n percent: '1' } ]\n
我想将此字符串转换为 Python 3 字典或 json。
【问题讨论】:
-
from ast import literal_eval然后literal_eval(a)- 这行得通吗? -
我试过错误
ValueError: malformed node or string: <_ast.Name object at 0x037513D0> -
你能发布更长的输出字符串吗?
-
添加真正的大字符串
标签: python string dictionary subprocess