我想将此字符串转换为JSON字典:
{u'Processes': [[u'root', u'3606', u'0.0', u'0.2', u'76768', u'16664', u'?', u'Ss', u'20:40', u'0:01', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'4088', u'0.0', u'0.2', u'88544', u'20156', u'?', u'S', u'20:40', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'4090', u'0.0', u'0.2', u'88552', u'20140', u'?', u'S', u'20:40', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'4097', u'0.0', u'0.2', u'88552', u'20112', u'?', u'S', u'20:40', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'4110', u'0.0', u'0.2', u'88548', u'20160', u'?', u'S', u'20:40', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0']], u'Titles': [u'USER', u'PID', u'%CPU', u'%MEM', u'VSZ', u'RSS', u'TTY', u'STAT', u'START', u'TIME', u'COMMAND']}
所以我使用了json.dumps和json.loads,但输出不是有效的JSON。 我知道JSON需要双引号而不是简单的引号,但我不认为解决方案是搜索和替换是解决此问题的最佳方法。 有没有更合适的方法呢?
答案 0 :(得分:2)
使用ast.literal_eval
将字符串转换为有效的Python对象。
安全地评估表达式节点或包含Python的字符串 文字或容器显示。提供的字符串或节点可能只是 由以下Python文字结构组成:字符串,字节, 数字,元组,列表,dicts,集合,布尔值和无。
s = "{u'Processes': [[u'root', u'3606', u'0.0', u'0.2', u'76768', u'16664', u'?', u'Ss', u'20:40', u'0:01', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'4088', u'0.0', u'0.2', u'88544', u'20156', u'?', u'S', u'20:40', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'4090', u'0.0', u'0.2', u'88552', u'20140', u'?', u'S', u'20:40', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'4097', u'0.0', u'0.2', u'88552', u'20112', u'?', u'S', u'20:40', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'4110', u'0.0', u'0.2', u'88548', u'20160', u'?', u'S', u'20:40', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0']], u'Titles': [u'USER', u'PID', u'%CPU', u'%MEM', u'VSZ', u'RSS', u'TTY', u'STAT', u'START', u'TIME', u'COMMAND']}"
o = ast.literal_eval(s)
assert 'Processes' in o
使用json.dumps
将其转储为JSON字符串。
import json
json.dumps(o)
# '{"Titles": ["USER", "PID", "%CPU", "%MEM", "VSZ", "RSS", "TTY", "STAT", "START", "TIME", "COMMAND"], "Processes": [["root", "3606", "0.0", "0.2", "76768", "16664", "?", "Ss", "20:40", "0:01", "/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0"], ["root", "4088", "0.0", "0.2", "88544", "20156", "?", "S", "20:40", "0:00", "/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0"], ["root", "4090", "0.0", "0.2", "88552", "20140", "?", "S", "20:40", "0:00", "/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0"], ["root", "4097", "0.0", "0.2", "88552", "20112", "?", "S", "20:40", "0:00", "/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0"], ["root", "4110", "0.0", "0.2", "88548", "20160", "?", "S", "20:40", "0:00", "/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0"]]}'
或者使用json.dump
将其转储到文件对象,如果这是你想要的。
答案 1 :(得分:1)
试试这个,
import json
data = {u'Processes': [[u'root', u'3606', u'0.0', u'0.2', u'76768', u'16664', u'?', u'Ss', u'20:40', u'0:01', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'4088', u'0.0', u'0.2', u'88544', u'20156', u'?', u'S', u'20:40', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'4090', u'0.0', u'0.2', u'88552', u'20140', u'?', u'S', u'20:40', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'4097', u'0.0', u'0.2', u'88552', u'20112', u'?', u'S', u'20:40', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0'], [u'root', u'4110', u'0.0', u'0.2', u'88548', u'20160', u'?', u'S', u'20:40', u'0:00', u'/usr/local/bin/python2 /usr/local/bin/gunicorn app:app -b 0.0.0.0:80 --log-file - --access-logfile - --workers 4 --keep-alive 0']], u'Titles': [u'USER', u'PID', u'%CPU', u'%MEM', u'VSZ', u'RSS', u'TTY', u'STAT', u'START', u'TIME', u'COMMAND']}
data = json.dumps(data) # dict to string
data = json.loads(data) # string to json
print data['Processes']