如何转换类型为' str'到了' dict'类型

时间:2018-04-12 13:01:51

标签: python python-2.7

我有一个文本文件' journal.txt'以下内容:

{ "__CURSOR" : "s=b0110bcb6bd44e8394728027cb5d82ce;i=b06;b=8ee885ed17a141abb35b8ef4903e8a41;m=158e9e2920;t=569a4d5ac3942;x=d80562ac11b34953", "__REALTIME_TIMESTAMP" : "1523531563940162", "__MONOTONIC_TIMESTAMP" : "92587043104", "_BOOT_ID" : "8ee885ed17a141abb35b8ef4903e8a41", "SYSLOG_FACILITY" : "3", "PRIORITY" : "6", "_UID" : "0", "_GID" : "0", "_MACHINE_ID" : "07c42c0296d34aea8b37c0acb2c260a8", "SYSLOG_IDENTIFIER" : "systemd", "_TRANSPORT" : "journal", "CODE_FILE" : "../git/src/core/job.c", "CODE_LINE" : "803", "CODE_FUNCTION" : "job_log_status_message", "MESSAGE_ID" : "39f53479d3a045ac8e11786248231fbf", "RESULT" : "done", "_PID" : "1", "_COMM" : "systemd", "_EXE" : "/lib/systemd/systemd", "_CMDLINE" : "/sbin/init", "_CAP_EFFECTIVE" : "3fffffffff", "_SYSTEMD_CGROUP" : "/init.scope", "_SYSTEMD_UNIT" : "init.scope", "_SYSTEMD_SLICE" : "-.slice", "_HOSTNAME" : "tait-uvh-7381982", "UNIT" : "audiorotator.service", "MESSAGE" : "Started Rotates audio files if they have filled up above the high tide mark..", "_SOURCE_REALTIME_TIMESTAMP" : "1523531563938118" } { "__CURSOR" : "s=b0110bcb6bd44e8394728027cb5d82ce;i=b07;b=8ee885ed17a141abb35b8ef4903e8a41;m=158ed7d220;t=569a4d5e5e241;x=cb77921bef1419a1", "__REALTIME_TIMESTAMP" : "1523531567718977", "__MONOTONIC_TIMESTAMP" : "92590821920", "_BOOT_ID" : "8ee885ed17a141abb35b8ef4903e8a41", "_UID" : "0", "_GID" : "0", "_SYSTEMD_SLICE" : "system.slice", "_MACHINE_ID" : "07c42c0296d34aea8b37c0acb2c260a8", "PRIORITY" : "7", "_CAP_EFFECTIVE" : "3fffffffff", "_HOSTNAME" : "tait-uvh-7381982", "_TRANSPORT" : "syslog", "_EXE" : "/usr/bin/python2.7", "SYSLOG_FACILITY" : "1", "_COMM" : "python", "SYSLOG_IDENTIFIER" : "audiorotator", "MESSAGE" : "Starting audio rotator", "_CMDLINE" : "python /usr/bin/audiorotator", "_SYSTEMD_CGROUP" : "/system.slice/audiorotator.service", "_SYSTEMD_UNIT" : "audiorotator.service", "_PID" : "5202", "_SOURCE_REALTIME_TIMESTAMP" : "1523531567716398" } { "__CURSOR" : "s=b0110bcb6bd44e8394728027cb5d82ce;i=b08;b=8ee885ed17a141abb35b8ef4903e8a41;m=158ed844f4;t=569a4d5e65515;x=c5fcb629064af9c3", "__REALTIME_TIMESTAMP" : "1523531567748373", "__MONOTONIC_TIMESTAMP" : "92590851316", "_BOOT_ID" : "8ee885ed17a141abb35b8ef4903e8a41", "_UID" : "0", "_GID" : "0", "_SYSTEMD_SLICE" : "system.slice", "_MACHINE_ID" : "07c42c0296d34aea8b37c0acb2c260a8", "PRIORITY" : "7", "_CAP_EFFECTIVE" : "3fffffffff", "_HOSTNAME" : "tait-uvh-7381982", "_TRANSPORT" : "syslog", "_EXE" : "/usr/bin/python2.7", "SYSLOG_FACILITY" : "1", "_COMM" : "python", "SYSLOG_IDENTIFIER" : "audiorotator", "_CMDLINE" : "python /usr/bin/audiorotator", "_SYSTEMD_CGROUP" : "/system.slice/audiorotator.service", "_SYSTEMD_UNIT" : "audiorotator.service", "MESSAGE" : "Audio rotation not required", "_PID" : "5202", "_SOURCE_REALTIME_TIMESTAMP" : "1523531567745888" } { "__CURSOR" : "s=b0110bcb6bd44e8394728027cb5d82ce;i=b09;b=8ee885ed17a141abb35b8ef4903e8a41;m=158ffe9ee8;t=569a4d70caf09;x=f669567bd2742aaf", "__REALTIME_TIMESTAMP" : "1523531587038985", "__MONOTONIC_TIMESTAMP" : "92610141928", "_BOOT_ID" : "8ee885ed17a141abb35b8ef4903e8a41", "SYSLOG_FACILITY" : "3", "PRIORITY" : "6", "_UID" : "0", "_GID" : "0", "_SYSTEMD_SLICE" : "system.slice", "_MACHINE_ID" : "07c42c0296d34aea8b37c0acb2c260a8", "_CAP_EFFECTIVE" : "3fffffffff", "_TRANSPORT" : "stdout", "_HOSTNAME" : "tait-uvh-7381982", "_EXE" : "/usr/bin/python2.7", "SYSLOG_IDENTIFIER" : "admin-webui.fcgi", "_PID" : "480", "_COMM" : "admin-webui.fcg", "_CMDLINE" : "/usr/bin/python /usr/share/www/admin-webui.fcgi", "_SYSTEMD_CGROUP" : "/system.slice/wsgiadminwebuiserver.service", "_SYSTEMD_UNIT" : "wsgiadminwebuiserver.service", "MESSAGE" : "active" } { "__CURSOR" : "s=b0110bcb6bd44e8394728027cb5d82ce;i=b0a;b=8ee885ed17a141abb35b8ef4903e8a41;m=159002f9e4;t=569a4d7110a05;x=f669567bd2742aaf", "__REALTIME_TIMESTAMP" : "1523531587324421", "__MONOTONIC_TIMESTAMP" : "92610427364", "_BOOT_ID" : "8ee885ed17a141abb35b8ef4903e8a41", "SYSLOG_FACILITY" : "3", "PRIORITY" : "6", "_UID" : "0", "_GID" : "0", "_SYSTEMD_SLICE" : "system.slice", "_MACHINE_ID" : "07c42c0296d34aea8b37c0acb2c260a8", "_CAP_EFFECTIVE" : "3fffffffff", "_TRANSPORT" : "stdout", "_HOSTNAME" : "tait-uvh-7381982", "_EXE" : "/usr/bin/python2.7", "SYSLOG_IDENTIFIER" : "admin-webui.fcgi", "_PID" : "480", "_COMM" : "admin-webui.fcg", "_CMDLINE" : "/usr/bin/python /usr/share/www/admin-webui.fcgi", "_SYSTEMD_CGROUP" : "/system.slice/wsgiadminwebuiserver.service", "_SYSTEMD_UNIT" : "wsgiadminwebuiserver.service", "MESSAGE" : "active" } { "__CURSOR" : "s=b0110bcb6bd44e8394728027cb5d82ce;i=b0b;b=8ee885ed17a141abb35b8ef4903e8a41;m=15901195eb;t=569a4d71fa60c;x=f669567bd2742aaf", "__REALTIME_TIMESTAMP" : "1523531588281868", "__MONOTONIC_TIMESTAMP" : "92611384811", "_BOOT_ID" : "8ee885ed17a141abb35b8ef4903e8a41", "SYSLOG_FACILITY" : "3", "PRIORITY" : "6", "_UID" : "0", "_GID" : "0", "_SYSTEMD_SLICE" : "system.slice", "_MACHINE_ID" : "07c42c0296d34aea8b37c0acb2c260a8", "_CAP_EFFECTIVE" : "3fffffffff", "_TRANSPORT" : "stdout", "_HOSTNAME" : "tait-uvh-7381982", "_EXE" : "/usr/bin/python2.7", "SYSLOG_IDENTIFIER" : "admin-webui.fcgi", "_PID" : "480", "_COMM" : "admin-webui.fcg", "_CMDLINE" : "/usr/bin/python /usr/share/www/admin-webui.fcgi", "_SYSTEMD_CGROUP" : "/system.slice/wsgiadminwebuiserver.service", "_SYSTEMD_UNIT" : "wsgiadminwebuiserver.service", "MESSAGE" : "active" } { "__CURSOR" : "s=b0110bcb6bd44e8394728027cb5d82ce;i=b0c;b=8ee885ed17a141abb35b8ef4903e8a41;m=15901654f2;t=569a4d7246513;x=f669567bd2742aaf", "__REALTIME_TIMESTAMP" : "1523531588592915", "__MONOTONIC_TIMESTAMP" : "92611695858", "_BOOT_ID" : "8ee885ed17a141abb35b8ef4903e8a41", "SYSLOG_FACILITY" : "3", "PRIORITY" : "6", "_UID" : "0", "_GID" : "0", "_SYSTEMD_SLICE" : "system.slice", "_MACHINE_ID" : "07c42c0296d34aea8b37c0acb2c260a8", "_CAP_EFFECTIVE" : "3fffffffff", "_TRANSPORT" : "stdout", "_HOSTNAME" : "tait-uvh-7381982", "_EXE" : "/usr/bin/python2.7", "SYSLOG_IDENTIFIER" : "admin-webui.fcgi", "_PID" : "480", "_COMM" : "admin-webui.fcg", "_CMDLINE" : "/usr/bin/python /usr/share/www/admin-webui.fcgi", "_SYSTEMD_CGROUP" : "/system.slice/wsgiadminwebuiserver.service", "_SYSTEMD_UNIT" : "wsgiadminwebuiserver.service", "MESSAGE" : "active" }

我正在尝试阅读上面的文件(多个字典元素但类型为str)并将内容转换为字典,期望类型为dict

到目前为止,我已尝试过以下方法,但未成功!

方法1:使用json.loads()

with open('C:\\Users\\billava\\Downloads\\journal.txt', 'r') as f:
    data = f.read()
    type(data)
    dict_data = json.loads(data)
    print dict_data


<type 'str'>

Traceback (most recent call last):
  File "<pyshell#80>", line 4, in <module>
    dict_data = json.loads(data)
  File "C:\Python27\lib\json\__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "C:\Python27\lib\json\decoder.py", line 367, in decode
    raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 1 column 1071 - line 1 column 6203 (char 1070 - 6202)

方法2:使用ast.literal_eval()

with open('C:\\Users\\billava\\Downloads\\journal.txt', 'r') as f:
    data = f.read()
    type(data)
    dict_data = ast.literal_eval(data)
    print dict_data


<type 'str'>

Traceback (most recent call last):
  File "<pyshell#82>", line 4, in <module>
    dict_data = ast.literal_eval(data)
  File "C:\Python27\lib\ast.py", line 49, in literal_eval
    node_or_string = parse(node_or_string, mode='eval')
  File "C:\Python27\lib\ast.py", line 37, in parse
    return compile(source, filename, mode, PyCF_ONLY_AST)
  File "<unknown>", line 1
    { "__CURSOR" : "s=b0110bcb6bd44e8394728027cb5d82ce;i=b06;b=8ee885ed17a141abb35b8ef4903e8a41;m=158e9e2920;t=569a4d5ac3942;x=d80562ac11b34953", "__REALTIME_TIMESTAMP" : "1523531563940162", "__MONOTONIC_TIMESTAMP" : "92587043104", "_BOOT_ID" : "8ee885ed17a141abb35b8ef4903e8a41", "SYSLOG_FACILITY" : "3", "PRIORITY" : "6", "_UID" : "0", "_GID" : "0", "_MACHINE_ID" : "07c42c0296d34aea8b37c0acb2c260a8", "SYSLOG_IDENTIFIER" : "systemd", "_TRANSPORT" : "journal", "CODE_FILE" : "../git/src/core/job.c", "CODE_LINE" : "803", "CODE_FUNCTION" : "job_log_status_message", "MESSAGE_ID" : "39f53479d3a045ac8e11786248231fbf", "RESULT" : "done", "_PID" : "1", "_COMM" : "systemd", "_EXE" : "/lib/systemd/systemd", "_CMDLINE" : "/sbin/init", "_CAP_EFFECTIVE" : "3fffffffff", "_SYSTEMD_CGROUP" : "/init.scope", "_SYSTEMD_UNIT" : "init.scope", "_SYSTEMD_SLICE" : "-.slice", "_HOSTNAME" : "tait-uvh-7381982", "UNIT" : "audiorotator.service", "MESSAGE" : "Started Rotates audio files if they have filled up above the high tide mark..", "_SOURCE_REALTIME_TIMESTAMP" : "1523531563938118" } { "__CURSOR" : "s=b0110bcb6bd44e8394728027cb5d82ce;i=b07;b=8ee885ed17a141abb35b8ef4903e8a41;m=158ed7d220;t=569a4d5e5e241;x=cb77921bef1419a1", "__REALTIME_TIMESTAMP" : "1523531567718977", "__MONOTONIC_TIMESTAMP" : "92590821920", "_BOOT_ID" : "8ee885ed17a141abb35b8ef4903e8a41", "_UID" : "0", "_GID" : "0", "_SYSTEMD_SLICE" : "system.slice", "_MACHINE_ID" : "07c42c0296d34aea8b37c0acb2c260a8", "PRIORITY" : "7", "_CAP_EFFECTIVE" : "3fffffffff", "_HOSTNAME" : "tait-uvh-7381982", "_TRANSPORT" : "syslog", "_EXE" : "/usr/bin/python2.7", "SYSLOG_FACILITY" : "1", "_COMM" : "python", "SYSLOG_IDENTIFIER" : "audiorotator", "MESSAGE" : "Starting audio rotator", "_CMDLINE" : "python /usr/bin/audiorotator", "_SYSTEMD_CGROUP" : "/system.slice/audiorotator.service", "_SYSTEMD_UNIT" : "audiorotator.service", "_PID" : "5202", "_SOURCE_REALTIME_TIMESTAMP" : "1523531567716398" } { "__CURSOR" : "s=b0110bcb6bd44e8394728027cb5d82ce;i=b08;b=8ee885ed17a141abb35b8ef4903e8a41;m=158ed844f4;t=569a4d5e65515;x=c5fcb629064af9c3", "__REALTIME_TIMESTAMP" : "1523531567748373", "__MONOTONIC_TIMESTAMP" : "92590851316", "_BOOT_ID" : "8ee885ed17a141abb35b8ef4903e8a41", "_UID" : "0", "_GID" : "0", "_SYSTEMD_SLICE" : "system.slice", "_MACHINE_ID" : "07c42c0296d34aea8b37c0acb2c260a8", "PRIORITY" : "7", "_CAP_EFFECTIVE" : "3fffffffff", "_HOSTNAME" : "tait-uvh-7381982", "_TRANSPORT" : "syslog", "_EXE" : "/usr/bin/python2.7", "SYSLOG_FACILITY" : "1", "_COMM" : "python", "SYSLOG_IDENTIFIER" : "audiorotator", "_CMDLINE" : "python /usr/bin/audiorotator", "_SYSTEMD_CGROUP" : "/system.slice/audiorotator.service", "_SYSTEMD_UNIT" : "audiorotator.service", "MESSAGE" : "Audio rotation not required", "_PID" : "5202", "_SOURCE_REALTIME_TIMESTAMP" : "1523531567745888" } { "__CURSOR" : "s=b0110bcb6bd44e8394728027cb5d82ce;i=b09;b=8ee885ed17a141abb35b8ef4903e8a41;m=158ffe9ee8;t=569a4d70caf09;x=f669567bd2742aaf", "__REALTIME_TIMESTAMP" : "1523531587038985", "__MONOTONIC_TIMESTAMP" : "92610141928", "_BOOT_ID" : "8ee885ed17a141abb35b8ef4903e8a41", "SYSLOG_FACILITY" : "3", "PRIORITY" : "6", "_UID" : "0", "_GID" : "0", "_SYSTEMD_SLICE" : "system.slice", "_MACHINE_ID" : "07c42c0296d34aea8b37c0acb2c260a8", "_CAP_EFFECTIVE" : "3fffffffff", "_TRANSPORT" : "stdout", "_HOSTNAME" : "tait-uvh-7381982", "_EXE" : "/usr/bin/python2.7", "SYSLOG_IDENTIFIER" : "admin-webui.fcgi", "_PID" : "480", "_COMM" : "admin-webui.fcg", "_CMDLINE" : "/usr/bin/python /usr/share/www/admin-webui.fcgi", "_SYSTEMD_CGROUP" : "/system.slice/wsgiadminwebuiserver.service", "_SYSTEMD_UNIT" : "wsgiadminwebuiserver.service", "MESSAGE" : "active" } { "__CURSOR" : "s=b0110bcb6bd44e8394728027cb5d82ce;i=b0a;b=8ee885ed17a141abb35b8ef4903e8a41;m=159002f9e4;t=569a4d7110a05;x=f669567bd2742aaf", "__REALTIME_TIMESTAMP" : "1523531587324421", "__MONOTONIC_TIMESTAMP" : "92610427364", "_BOOT_ID" : "8ee885ed17a141abb35b8ef4903e8a41", "SYSLOG_FACILITY" : "3", "PRIORITY" : "6", "_UID" : "0", "_GID" : "0", "_SYSTEMD_SLICE" : "system.slice", "_MACHINE_ID" : "07c42c0296d34aea8b37c0acb2c260a8", "_CAP_EFFECTIVE" : "3fffffffff", "_TRANSPORT" : "stdout", "_HOSTNAME" : "tait-uvh-7381982", "_EXE" : "/usr/bin/python2.7", "SYSLOG_IDENTIFIER" : "admin-webui.fcgi", "_PID" : "480", "_COMM" : "admin-webui.fcg", "_CMDLINE" : "/usr/bin/python /usr/share/www/admin-webui.fcgi", "_SYSTEMD_CGROUP" : "/system.slice/wsgiadminwebuiserver.service", "_SYSTEMD_UNIT" : "wsgiadminwebuiserver.service", "MESSAGE" : "active" } { "__CURSOR" : "s=b0110bcb6bd44e8394728027cb5d82ce;i=b0b;b=8ee885ed17a141abb35b8ef4903e8a41;m=15901195eb;t=569a4d71fa60c;x=f669567bd2742aaf", "__REALTIME_TIMESTAMP" : "1523531588281868", "__MONOTONIC_TIMESTAMP" : "92611384811", "_BOOT_ID" : "8ee885ed17a141abb35b8ef4903e8a41", "SYSLOG_FACILITY" : "3", "PRIORITY" : "6", "_UID" : "0", "_GID" : "0", "_SYSTEMD_SLICE" : "system.slice", "_MACHINE_ID" : "07c42c0296d34aea8b37c0acb2c260a8", "_CAP_EFFECTIVE" : "3fffffffff", "_TRANSPORT" : "stdout", "_HOSTNAME" : "tait-uvh-7381982", "_EXE" : "/usr/bin/python2.7", "SYSLOG_IDENTIFIER" : "admin-webui.fcgi", "_PID" : "480", "_COMM" : "admin-webui.fcg", "_CMDLINE" : "/usr/bin/python /usr/share/www/admin-webui.fcgi", "_SYSTEMD_CGROUP" : "/system.slice/wsgiadminwebuiserver.service", "_SYSTEMD_UNIT" : "wsgiadminwebuiserver.service", "MESSAGE" : "active" } { "__CURSOR" : "s=b0110bcb6bd44e8394728027cb5d82ce;i=b0c;b=8ee885ed17a141abb35b8ef4903e8a41;m=15901654f2;t=569a4d7246513;x=f669567bd2742aaf", "__REALTIME_TIMESTAMP" : "1523531588592915", "__MONOTONIC_TIMESTAMP" : "92611695858", "_BOOT_ID" : "8ee885ed17a141abb35b8ef4903e8a41", "SYSLOG_FACILITY" : "3", "PRIORITY" : "6", "_UID" : "0", "_GID" : "0", "_SYSTEMD_SLICE" : "system.slice", "_MACHINE_ID" : "07c42c0296d34aea8b37c0acb2c260a8", "_CAP_EFFECTIVE" : "3fffffffff", "_TRANSPORT" : "stdout", "_HOSTNAME" : "tait-uvh-7381982", "_EXE" : "/usr/bin/python2.7", "SYSLOG_IDENTIFIER" : "admin-webui.fcgi", "_PID" : "480", "_COMM" : "admin-webui.fcg", "_CMDLINE" : "/usr/bin/python /usr/share/www/admin-webui.fcgi", "_SYSTEMD_CGROUP" : "/system.slice/wsgiadminwebuiserver.service", "_SYSTEMD_UNIT" : "wsgiadminwebuiserver.service", "MESSAGE" : "active" }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  ^
SyntaxError: invalid syntax

4 个答案:

答案 0 :(得分:3)

使用正则表达式获取大括号之间的内容,然后解析它。

<强>演示

import json
import re
with open('C:\\Users\\billava\\Downloads\\journal.txt', 'r') as f:
    data = re.findall("\{(.*?)\}", f.read())
    for i in data:
        print json.loads("{" + i + "}")

<强>输出:

{u'_BOOT_ID': u'8ee885ed17a141abb35b8ef4903e8a41', u'__MONOTONIC_TIMESTAMP': u'92587043104', u'SYSLOG_FACILITY': u'3', u'CODE_LINE': u'803', u'CODE_FUNCTION': u'job_log_status_message', u'__REALTIME_TIMESTAMP': u'1523531563940162', u'PRIORITY': u'6', u'_TRANSPORT': u'journal', u'_HOSTNAME': u'tait-uvh-7381982', u'_CAP_EFFECTIVE': u'3fffffffff', u'_SYSTEMD_UNIT': u'init.scope', u'_MACHINE_ID': u'07c42c0296d34aea8b37c0acb2c260a8', u'_PID': u'1', u'SYSLOG_IDENTIFIER': u'systemd', u'_SOURCE_REALTIME_TIMESTAMP': u'1523531563938118', u'_SYSTEMD_CGROUP': u'/init.scope', u'__CURSOR': u's=b0110bcb6bd44e8394728027cb5d82ce;i=b06;b=8ee885ed17a141abb35b8ef4903e8a41;m=158e9e2920;t=569a4d5ac3942;x=d80562ac11b34953', u'RESULT': u'done', u'MESSAGE_ID': u'39f53479d3a045ac8e11786248231fbf', u'_COMM': u'systemd', u'CODE_FILE': u'../git/src/core/job.c', u'_CMDLINE': u'/sbin/init', u'_SYSTEMD_SLICE': u'-.slice', u'UNIT': u'audiorotator.service', u'_EXE': u'/lib/systemd/systemd', u'_UID': u'0', u'MESSAGE': u'Started Rotates audio files if they have filled up above the high tide mark..', u'_GID': u'0'}
{u'_BOOT_ID': u'8ee885ed17a141abb35b8ef4903e8a41', u'__MONOTONIC_TIMESTAMP': u'92590821920', u'SYSLOG_FACILITY': u'1', u'_GID': u'0', u'__REALTIME_TIMESTAMP': u'1523531567718977', u'PRIORITY': u'7', u'_TRANSPORT': u'syslog', u'_HOSTNAME': u'tait-uvh-7381982', u'_CAP_EFFECTIVE': u'3fffffffff', u'_SYSTEMD_UNIT': u'audiorotator.service', u'_MACHINE_ID': u'07c42c0296d34aea8b37c0acb2c260a8', u'_PID': u'5202', u'SYSLOG_IDENTIFIER': u'audiorotator', u'_SOURCE_REALTIME_TIMESTAMP': u'1523531567716398', u'_SYSTEMD_CGROUP': u'/system.slice/audiorotator.service', u'__CURSOR': u's=b0110bcb6bd44e8394728027cb5d82ce;i=b07;b=8ee885ed17a141abb35b8ef4903e8a41;m=158ed7d220;t=569a4d5e5e241;x=cb77921bef1419a1', u'_COMM': u'python', u'_CMDLINE': u'python /usr/bin/audiorotator', u'_SYSTEMD_SLICE': u'system.slice', u'_EXE': u'/usr/bin/python2.7', u'_UID': u'0', u'MESSAGE': u'Starting audio rotator'}
{u'_BOOT_ID': u'8ee885ed17a141abb35b8ef4903e8a41', u'__MONOTONIC_TIMESTAMP': u'92590851316', u'SYSLOG_FACILITY': u'1', u'_GID': u'0', u'__REALTIME_TIMESTAMP': u'1523531567748373', u'PRIORITY': u'7', u'_TRANSPORT': u'syslog', u'_HOSTNAME': u'tait-uvh-7381982', u'_CAP_EFFECTIVE': u'3fffffffff', u'_SYSTEMD_UNIT': u'audiorotator.service', u'_MACHINE_ID': u'07c42c0296d34aea8b37c0acb2c260a8', u'_PID': u'5202', u'SYSLOG_IDENTIFIER': u'audiorotator', u'_SOURCE_REALTIME_TIMESTAMP': u'1523531567745888', u'_SYSTEMD_CGROUP': u'/system.slice/audiorotator.service', u'__CURSOR': u's=b0110bcb6bd44e8394728027cb5d82ce;i=b08;b=8ee885ed17a141abb35b8ef4903e8a41;m=158ed844f4;t=569a4d5e65515;x=c5fcb629064af9c3', u'_COMM': u'python', u'_CMDLINE': u'python /usr/bin/audiorotator', u'_SYSTEMD_SLICE': u'system.slice', u'_EXE': u'/usr/bin/python2.7', u'_UID': u'0', u'MESSAGE': u'Audio rotation not required'}
{u'_BOOT_ID': u'8ee885ed17a141abb35b8ef4903e8a41', u'__REALTIME_TIMESTAMP': u'1523531587038985', u'_CAP_EFFECTIVE': u'3fffffffff', u'__MONOTONIC_TIMESTAMP': u'92610141928', u'_SYSTEMD_UNIT': u'wsgiadminwebuiserver.service', u'_MACHINE_ID': u'07c42c0296d34aea8b37c0acb2c260a8', u'_PID': u'480', u'_CMDLINE': u'/usr/bin/python /usr/share/www/admin-webui.fcgi', u'_SYSTEMD_CGROUP': u'/system.slice/wsgiadminwebuiserver.service', u'_SYSTEMD_SLICE': u'system.slice', u'SYSLOG_FACILITY': u'3', u'PRIORITY': u'6', u'_EXE': u'/usr/bin/python2.7', u'SYSLOG_IDENTIFIER': u'admin-webui.fcgi', u'_UID': u'0', u'__CURSOR': u's=b0110bcb6bd44e8394728027cb5d82ce;i=b09;b=8ee885ed17a141abb35b8ef4903e8a41;m=158ffe9ee8;t=569a4d70caf09;x=f669567bd2742aaf', u'_GID': u'0', u'_TRANSPORT': u'stdout', u'MESSAGE': u'active', u'_COMM': u'admin-webui.fcg', u'_HOSTNAME': u'tait-uvh-7381982'}
{u'_BOOT_ID': u'8ee885ed17a141abb35b8ef4903e8a41', u'__REALTIME_TIMESTAMP': u'1523531587324421', u'_CAP_EFFECTIVE': u'3fffffffff', u'__MONOTONIC_TIMESTAMP': u'92610427364', u'_SYSTEMD_UNIT': u'wsgiadminwebuiserver.service', u'_MACHINE_ID': u'07c42c0296d34aea8b37c0acb2c260a8', u'_PID': u'480', u'_CMDLINE': u'/usr/bin/python /usr/share/www/admin-webui.fcgi', u'_SYSTEMD_CGROUP': u'/system.slice/wsgiadminwebuiserver.service', u'_SYSTEMD_SLICE': u'system.slice', u'SYSLOG_FACILITY': u'3', u'PRIORITY': u'6', u'_EXE': u'/usr/bin/python2.7', u'SYSLOG_IDENTIFIER': u'admin-webui.fcgi', u'_UID': u'0', u'__CURSOR': u's=b0110bcb6bd44e8394728027cb5d82ce;i=b0a;b=8ee885ed17a141abb35b8ef4903e8a41;m=159002f9e4;t=569a4d7110a05;x=f669567bd2742aaf', u'_GID': u'0', u'_TRANSPORT': u'stdout', u'MESSAGE': u'active', u'_COMM': u'admin-webui.fcg', u'_HOSTNAME': u'tait-uvh-7381982'}
{u'_BOOT_ID': u'8ee885ed17a141abb35b8ef4903e8a41', u'__REALTIME_TIMESTAMP': u'1523531588281868', u'_CAP_EFFECTIVE': u'3fffffffff', u'__MONOTONIC_TIMESTAMP': u'92611384811', u'_SYSTEMD_UNIT': u'wsgiadminwebuiserver.service', u'_MACHINE_ID': u'07c42c0296d34aea8b37c0acb2c260a8', u'_PID': u'480', u'_CMDLINE': u'/usr/bin/python /usr/share/www/admin-webui.fcgi', u'_SYSTEMD_CGROUP': u'/system.slice/wsgiadminwebuiserver.service', u'_SYSTEMD_SLICE': u'system.slice', u'SYSLOG_FACILITY': u'3', u'PRIORITY': u'6', u'_EXE': u'/usr/bin/python2.7', u'SYSLOG_IDENTIFIER': u'admin-webui.fcgi', u'_UID': u'0', u'__CURSOR': u's=b0110bcb6bd44e8394728027cb5d82ce;i=b0b;b=8ee885ed17a141abb35b8ef4903e8a41;m=15901195eb;t=569a4d71fa60c;x=f669567bd2742aaf', u'_GID': u'0', u'_TRANSPORT': u'stdout', u'MESSAGE': u'active', u'_COMM': u'admin-webui.fcg', u'_HOSTNAME': u'tait-uvh-7381982'}
{u'_BOOT_ID': u'8ee885ed17a141abb35b8ef4903e8a41', u'__REALTIME_TIMESTAMP': u'1523531588592915', u'_CAP_EFFECTIVE': u'3fffffffff', u'__MONOTONIC_TIMESTAMP': u'92611695858', u'_SYSTEMD_UNIT': u'wsgiadminwebuiserver.service', u'_MACHINE_ID': u'07c42c0296d34aea8b37c0acb2c260a8', u'_PID': u'480', u'_CMDLINE': u'/usr/bin/python /usr/share/www/admin-webui.fcgi', u'_SYSTEMD_CGROUP': u'/system.slice/wsgiadminwebuiserver.service', u'_SYSTEMD_SLICE': u'system.slice', u'SYSLOG_FACILITY': u'3', u'PRIORITY': u'6', u'_EXE': u'/usr/bin/python2.7', u'SYSLOG_IDENTIFIER': u'admin-webui.fcgi', u'_UID': u'0', u'__CURSOR': u's=b0110bcb6bd44e8394728027cb5d82ce;i=b0c;b=8ee885ed17a141abb35b8ef4903e8a41;m=15901654f2;t=569a4d7246513;x=f669567bd2742aaf', u'_GID': u'0', u'_TRANSPORT': u'stdout', u'MESSAGE': u'active', u'_COMM': u'admin-webui.fcg', u'_HOSTNAME': u'tait-uvh-7381982'}

答案 1 :(得分:1)

正如@Kenstars所提到的,你的字符串有多个字典元素json.loads()将不起作用。

一个技巧是将字符串作为下面的字典列表:

import json
data = '{ "__CURSOR" : "s=b0110bcb6bd44e8394728027cb5d82ce;i=b06;b=8ee885ed17a141abb35b8ef4903e8a41;m=158e9e2920;t=569a4d5ac3942;x=d80562ac11b34953", "__REALTIME_TIMESTAMP" : "1523531563940162", "__MONOTONIC_TIMESTAMP" : "92587043104", "_BOOT_ID" : "8ee885ed17a141abb35b8ef4903e8a41", "SYSLOG_FACILITY" : "3", "PRIORITY" : "6", "_UID" : "0", "_GID" : "0", "_MACHINE_ID" : "07c42c0296d34aea8b37c0acb2c260a8", "SYSLOG_IDENTIFIER" : "systemd", "_TRANSPORT" : "journal", "CODE_FILE" : "../git/src/core/job.c", "CODE_LINE" : "803", "CODE_FUNCTION" : "job_log_status_message", "MESSAGE_ID" : "39f53479d3a045ac8e11786248231fbf", "RESULT" : "done", "_PID" : "1", "_COMM" : "systemd", "_EXE" : "/lib/systemd/systemd", "_CMDLINE" : "/sbin/init", "_CAP_EFFECTIVE" : "3fffffffff", "_SYSTEMD_CGROUP" : "/init.scope", "_SYSTEMD_UNIT" : "init.scope", "_SYSTEMD_SLICE" : "-.slice", "_HOSTNAME" : "tait-uvh-7381982", "UNIT" : "audiorotator.service", "MESSAGE" : "Started Rotates audio files if they have filled up above the high tide mark..", "_SOURCE_REALTIME_TIMESTAMP" : "1523531563938118" } { "__CURSOR" : "s=b0110bcb6bd44e8394728027cb5d82ce;i=b07;b=8ee885ed17a141abb35b8ef4903e8a41;m=158ed7d220;t=569a4d5e5e241;x=cb77921bef1419a1", "__REALTIME_TIMESTAMP" : "1523531567718977", "__MONOTONIC_TIMESTAMP" : "92590821920", "_BOOT_ID" : "8ee885ed17a141abb35b8ef4903e8a41", "_UID" : "0", "_GID" : "0", "_SYSTEMD_SLICE" : "system.slice", "_MACHINE_ID" : "07c42c0296d34aea8b37c0acb2c260a8", "PRIORITY" : "7", "_CAP_EFFECTIVE" : "3fffffffff", "_HOSTNAME" : "tait-uvh-7381982", "_TRANSPORT" : "syslog", "_EXE" : "/usr/bin/python2.7", "SYSLOG_FACILITY" : "1", "_COMM" : "python", "SYSLOG_IDENTIFIER" : "audiorotator", "MESSAGE" : "Starting audio rotator", "_CMDLINE" : "python /usr/bin/audiorotator", "_SYSTEMD_CGROUP" : "/system.slice/audiorotator.service", "_SYSTEMD_UNIT" : "audiorotator.service", "_PID" : "5202", "_SOURCE_REALTIME_TIMESTAMP" : "1523531567716398" } { "__CURSOR" : "s=b0110bcb6bd44e8394728027cb5d82ce;i=b08;b=8ee885ed17a141abb35b8ef4903e8a41;m=158ed844f4;t=569a4d5e65515;x=c5fcb629064af9c3", "__REALTIME_TIMESTAMP" : "1523531567748373", "__MONOTONIC_TIMESTAMP" : "92590851316", "_BOOT_ID" : "8ee885ed17a141abb35b8ef4903e8a41", "_UID" : "0", "_GID" : "0", "_SYSTEMD_SLICE" : "system.slice", "_MACHINE_ID" : "07c42c0296d34aea8b37c0acb2c260a8", "PRIORITY" : "7", "_CAP_EFFECTIVE" : "3fffffffff", "_HOSTNAME" : "tait-uvh-7381982", "_TRANSPORT" : "syslog", "_EXE" : "/usr/bin/python2.7", "SYSLOG_FACILITY" : "1", "_COMM" : "python", "SYSLOG_IDENTIFIER" : "audiorotator", "_CMDLINE" : "python /usr/bin/audiorotator", "_SYSTEMD_CGROUP" : "/system.slice/audiorotator.service", "_SYSTEMD_UNIT" : "audiorotator.service", "MESSAGE" : "Audio rotation not required", "_PID" : "5202", "_SOURCE_REALTIME_TIMESTAMP" : "1523531567745888" } { "__CURSOR" : "s=b0110bcb6bd44e8394728027cb5d82ce;i=b09;b=8ee885ed17a141abb35b8ef4903e8a41;m=158ffe9ee8;t=569a4d70caf09;x=f669567bd2742aaf", "__REALTIME_TIMESTAMP" : "1523531587038985", "__MONOTONIC_TIMESTAMP" : "92610141928", "_BOOT_ID" : "8ee885ed17a141abb35b8ef4903e8a41", "SYSLOG_FACILITY" : "3", "PRIORITY" : "6", "_UID" : "0", "_GID" : "0", "_SYSTEMD_SLICE" : "system.slice", "_MACHINE_ID" : "07c42c0296d34aea8b37c0acb2c260a8", "_CAP_EFFECTIVE" : "3fffffffff", "_TRANSPORT" : "stdout", "_HOSTNAME" : "tait-uvh-7381982", "_EXE" : "/usr/bin/python2.7", "SYSLOG_IDENTIFIER" : "admin-webui.fcgi", "_PID" : "480", "_COMM" : "admin-webui.fcg", "_CMDLINE" : "/usr/bin/python /usr/share/www/admin-webui.fcgi", "_SYSTEMD_CGROUP" : "/system.slice/wsgiadminwebuiserver.service", "_SYSTEMD_UNIT" : "wsgiadminwebuiserver.service", "MESSAGE" : "active" } { "__CURSOR" : "s=b0110bcb6bd44e8394728027cb5d82ce;i=b0a;b=8ee885ed17a141abb35b8ef4903e8a41;m=159002f9e4;t=569a4d7110a05;x=f669567bd2742aaf", "__REALTIME_TIMESTAMP" : "1523531587324421", "__MONOTONIC_TIMESTAMP" : "92610427364", "_BOOT_ID" : "8ee885ed17a141abb35b8ef4903e8a41", "SYSLOG_FACILITY" : "3", "PRIORITY" : "6", "_UID" : "0", "_GID" : "0", "_SYSTEMD_SLICE" : "system.slice", "_MACHINE_ID" : "07c42c0296d34aea8b37c0acb2c260a8", "_CAP_EFFECTIVE" : "3fffffffff", "_TRANSPORT" : "stdout", "_HOSTNAME" : "tait-uvh-7381982", "_EXE" : "/usr/bin/python2.7", "SYSLOG_IDENTIFIER" : "admin-webui.fcgi", "_PID" : "480", "_COMM" : "admin-webui.fcg", "_CMDLINE" : "/usr/bin/python /usr/share/www/admin-webui.fcgi", "_SYSTEMD_CGROUP" : "/system.slice/wsgiadminwebuiserver.service", "_SYSTEMD_UNIT" : "wsgiadminwebuiserver.service", "MESSAGE" : "active" } { "__CURSOR" : "s=b0110bcb6bd44e8394728027cb5d82ce;i=b0b;b=8ee885ed17a141abb35b8ef4903e8a41;m=15901195eb;t=569a4d71fa60c;x=f669567bd2742aaf", "__REALTIME_TIMESTAMP" : "1523531588281868", "__MONOTONIC_TIMESTAMP" : "92611384811", "_BOOT_ID" : "8ee885ed17a141abb35b8ef4903e8a41", "SYSLOG_FACILITY" : "3", "PRIORITY" : "6", "_UID" : "0", "_GID" : "0", "_SYSTEMD_SLICE" : "system.slice", "_MACHINE_ID" : "07c42c0296d34aea8b37c0acb2c260a8", "_CAP_EFFECTIVE" : "3fffffffff", "_TRANSPORT" : "stdout", "_HOSTNAME" : "tait-uvh-7381982", "_EXE" : "/usr/bin/python2.7", "SYSLOG_IDENTIFIER" : "admin-webui.fcgi", "_PID" : "480", "_COMM" : "admin-webui.fcg", "_CMDLINE" : "/usr/bin/python /usr/share/www/admin-webui.fcgi", "_SYSTEMD_CGROUP" : "/system.slice/wsgiadminwebuiserver.service", "_SYSTEMD_UNIT" : "wsgiadminwebuiserver.service", "MESSAGE" : "active" } { "__CURSOR" : "s=b0110bcb6bd44e8394728027cb5d82ce;i=b0c;b=8ee885ed17a141abb35b8ef4903e8a41;m=15901654f2;t=569a4d7246513;x=f669567bd2742aaf", "__REALTIME_TIMESTAMP" : "1523531588592915", "__MONOTONIC_TIMESTAMP" : "92611695858", "_BOOT_ID" : "8ee885ed17a141abb35b8ef4903e8a41", "SYSLOG_FACILITY" : "3", "PRIORITY" : "6", "_UID" : "0", "_GID" : "0", "_SYSTEMD_SLICE" : "system.slice", "_MACHINE_ID" : "07c42c0296d34aea8b37c0acb2c260a8", "_CAP_EFFECTIVE" : "3fffffffff", "_TRANSPORT" : "stdout", "_HOSTNAME" : "tait-uvh-7381982", "_EXE" : "/usr/bin/python2.7", "SYSLOG_IDENTIFIER" : "admin-webui.fcgi", "_PID" : "480", "_COMM" : "admin-webui.fcg", "_CMDLINE" : "/usr/bin/python /usr/share/www/admin-webui.fcgi", "_SYSTEMD_CGROUP" : "/system.slice/wsgiadminwebuiserver.service", "_SYSTEMD_UNIT" : "wsgiadminwebuiserver.service", "MESSAGE" : "active" }'
data = data.replace("}","},")
data = '['+data[:-1]+']'
converted_data = json.loads(data)
print(converted_data)

输出:

[
    {
        "MESSAGE": "Started Rotates audio files if they have filled up above the high tide mark..",
        "_SOURCE_REALTIME_TIMESTAMP": "1523531563938118",
        "_SYSTEMD_SLICE": "-.slice",
        "_MACHINE_ID": "07c42c0296d34aea8b37c0acb2c260a8",
        "_COMM": "systemd",
        "_PID": "1",
        "__MONOTONIC_TIMESTAMP": "92587043104",
        "_UID": "0",
        "_CMDLINE": "/sbin/init",
        "CODE_FILE": "../git/src/core/job.c",
        "_TRANSPORT": "journal",
        "UNIT": "audiorotator.service",
        "__CURSOR": "s=b0110bcb6bd44e8394728027cb5d82ce;i=b06;b=8ee885ed17a141abb35b8ef4903e8a41;m=158e9e2920;t=569a4d5ac3942;x=d80562ac11b34953",
        "CODE_LINE": "803",
        "_SYSTEMD_UNIT": "init.scope",
        "MESSAGE_ID": "39f53479d3a045ac8e11786248231fbf",
        "_EXE": "/lib/systemd/systemd",
        "RESULT": "done",
        "SYSLOG_FACILITY": "3",
        "_SYSTEMD_CGROUP": "/init.scope",
        "SYSLOG_IDENTIFIER": "systemd",
        "CODE_FUNCTION": "job_log_status_message",
        "_BOOT_ID": "8ee885ed17a141abb35b8ef4903e8a41",
        "_CAP_EFFECTIVE": "3fffffffff",
        "__REALTIME_TIMESTAMP": "1523531563940162",
        "_GID": "0",
        "_HOSTNAME": "tait-uvh-7381982",
        "PRIORITY": "6"
    },
    {
        "MESSAGE": "Starting audio rotator",
        "_SYSTEMD_SLICE": "system.slice",
        "_MACHINE_ID": "07c42c0296d34aea8b37c0acb2c260a8",
        "_COMM": "python",
        "_PID": "5202",
        "_TRANSPORT": "syslog",
        "_UID": "0",
        "_CMDLINE": "python /usr/bin/audiorotator",
        "__MONOTONIC_TIMESTAMP": "92590821920",
        "__CURSOR": "s=b0110bcb6bd44e8394728027cb5d82ce;i=b07;b=8ee885ed17a141abb35b8ef4903e8a41;m=158ed7d220;t=569a4d5e5e241;x=cb77921bef1419a1",
        "_SYSTEMD_UNIT": "audiorotator.service",
        "_SOURCE_REALTIME_TIMESTAMP": "1523531567716398",
        "_EXE": "/usr/bin/python2.7",
        "SYSLOG_FACILITY": "1",
        "PRIORITY": "7",
        "SYSLOG_IDENTIFIER": "audiorotator",
        "_BOOT_ID": "8ee885ed17a141abb35b8ef4903e8a41",
        "_CAP_EFFECTIVE": "3fffffffff",
        "__REALTIME_TIMESTAMP": "1523531567718977",
        "_GID": "0",
        "_HOSTNAME": "tait-uvh-7381982",
        "_SYSTEMD_CGROUP": "/system.slice/audiorotator.service"
    },
    {
        "MESSAGE": "Audio rotation not required",
        "_SYSTEMD_SLICE": "system.slice",
        "_MACHINE_ID": "07c42c0296d34aea8b37c0acb2c260a8",
        "_COMM": "python",
        "_PID": "5202",
        "_TRANSPORT": "syslog",
        "_UID": "0",
        "_CMDLINE": "python /usr/bin/audiorotator",
        "__MONOTONIC_TIMESTAMP": "92590851316",
        "__CURSOR": "s=b0110bcb6bd44e8394728027cb5d82ce;i=b08;b=8ee885ed17a141abb35b8ef4903e8a41;m=158ed844f4;t=569a4d5e65515;x=c5fcb629064af9c3",
        "_SYSTEMD_UNIT": "audiorotator.service",
        "_SOURCE_REALTIME_TIMESTAMP": "1523531567745888",
        "_EXE": "/usr/bin/python2.7",
        "SYSLOG_FACILITY": "1",
        "PRIORITY": "7",
        "SYSLOG_IDENTIFIER": "audiorotator",
        "_BOOT_ID": "8ee885ed17a141abb35b8ef4903e8a41",
        "_CAP_EFFECTIVE": "3fffffffff",
        "__REALTIME_TIMESTAMP": "1523531567748373",
        "_GID": "0",
        "_HOSTNAME": "tait-uvh-7381982",
        "_SYSTEMD_CGROUP": "/system.slice/audiorotator.service"
    },
    {
        "MESSAGE": "active",
        "_SYSTEMD_UNIT": "wsgiadminwebuiserver.service",
        "_BOOT_ID": "8ee885ed17a141abb35b8ef4903e8a41",
        "_MACHINE_ID": "07c42c0296d34aea8b37c0acb2c260a8",
        "_COMM": "admin-webui.fcg",
        "_PID": "480",
        "_EXE": "/usr/bin/python2.7",
        "_CAP_EFFECTIVE": "3fffffffff",
        "_SYSTEMD_CGROUP": "/system.slice/wsgiadminwebuiserver.service",
        "SYSLOG_FACILITY": "3",
        "__CURSOR": "s=b0110bcb6bd44e8394728027cb5d82ce;i=b09;b=8ee885ed17a141abb35b8ef4903e8a41;m=158ffe9ee8;t=569a4d70caf09;x=f669567bd2742aaf",
        "_UID": "0",
        "_CMDLINE": "/usr/bin/python /usr/share/www/admin-webui.fcgi",
        "SYSLOG_IDENTIFIER": "admin-webui.fcgi",
        "_SYSTEMD_SLICE": "system.slice",
        "__MONOTONIC_TIMESTAMP": "92610141928",
        "__REALTIME_TIMESTAMP": "1523531587038985",
        "_GID": "0",
        "_HOSTNAME": "tait-uvh-7381982",
        "PRIORITY": "6",
        "_TRANSPORT": "stdout"
    },
    {
        "MESSAGE": "active",
        "_SYSTEMD_UNIT": "wsgiadminwebuiserver.service",
        "_BOOT_ID": "8ee885ed17a141abb35b8ef4903e8a41",
        "_MACHINE_ID": "07c42c0296d34aea8b37c0acb2c260a8",
        "_COMM": "admin-webui.fcg",
        "_PID": "480",
        "_EXE": "/usr/bin/python2.7",
        "_CAP_EFFECTIVE": "3fffffffff",
        "_SYSTEMD_CGROUP": "/system.slice/wsgiadminwebuiserver.service",
        "SYSLOG_FACILITY": "3",
        "__CURSOR": "s=b0110bcb6bd44e8394728027cb5d82ce;i=b0a;b=8ee885ed17a141abb35b8ef4903e8a41;m=159002f9e4;t=569a4d7110a05;x=f669567bd2742aaf",
        "_UID": "0",
        "_CMDLINE": "/usr/bin/python /usr/share/www/admin-webui.fcgi",
        "SYSLOG_IDENTIFIER": "admin-webui.fcgi",
        "_SYSTEMD_SLICE": "system.slice",
        "__MONOTONIC_TIMESTAMP": "92610427364",
        "__REALTIME_TIMESTAMP": "1523531587324421",
        "_GID": "0",
        "_HOSTNAME": "tait-uvh-7381982",
        "PRIORITY": "6",
        "_TRANSPORT": "stdout"
    },
    {
        "MESSAGE": "active",
        "_SYSTEMD_UNIT": "wsgiadminwebuiserver.service",
        "_BOOT_ID": "8ee885ed17a141abb35b8ef4903e8a41",
        "_MACHINE_ID": "07c42c0296d34aea8b37c0acb2c260a8",
        "_COMM": "admin-webui.fcg",
        "_PID": "480",
        "_EXE": "/usr/bin/python2.7",
        "_CAP_EFFECTIVE": "3fffffffff",
        "_SYSTEMD_CGROUP": "/system.slice/wsgiadminwebuiserver.service",
        "SYSLOG_FACILITY": "3",
        "__CURSOR": "s=b0110bcb6bd44e8394728027cb5d82ce;i=b0b;b=8ee885ed17a141abb35b8ef4903e8a41;m=15901195eb;t=569a4d71fa60c;x=f669567bd2742aaf",
        "_UID": "0",
        "_CMDLINE": "/usr/bin/python /usr/share/www/admin-webui.fcgi",
        "SYSLOG_IDENTIFIER": "admin-webui.fcgi",
        "_SYSTEMD_SLICE": "system.slice",
        "__MONOTONIC_TIMESTAMP": "92611384811",
        "__REALTIME_TIMESTAMP": "1523531588281868",
        "_GID": "0",
        "_HOSTNAME": "tait-uvh-7381982",
        "PRIORITY": "6",
        "_TRANSPORT": "stdout"
    },
    {
        "MESSAGE": "active",
        "_SYSTEMD_UNIT": "wsgiadminwebuiserver.service",
        "_BOOT_ID": "8ee885ed17a141abb35b8ef4903e8a41",
        "_MACHINE_ID": "07c42c0296d34aea8b37c0acb2c260a8",
        "_COMM": "admin-webui.fcg",
        "_PID": "480",
        "_EXE": "/usr/bin/python2.7",
        "_CAP_EFFECTIVE": "3fffffffff",
        "_SYSTEMD_CGROUP": "/system.slice/wsgiadminwebuiserver.service",
        "SYSLOG_FACILITY": "3",
        "__CURSOR": "s=b0110bcb6bd44e8394728027cb5d82ce;i=b0c;b=8ee885ed17a141abb35b8ef4903e8a41;m=15901654f2;t=569a4d7246513;x=f669567bd2742aaf",
        "_UID": "0",
        "_CMDLINE": "/usr/bin/python /usr/share/www/admin-webui.fcgi",
        "SYSLOG_IDENTIFIER": "admin-webui.fcgi",
        "_SYSTEMD_SLICE": "system.slice",
        "__MONOTONIC_TIMESTAMP": "92611695858",
        "__REALTIME_TIMESTAMP": "1523531588592915",
        "_GID": "0",
        "_HOSTNAME": "tait-uvh-7381982",
        "PRIORITY": "6",
        "_TRANSPORT": "stdout"
    }
]

答案 2 :(得分:0)

现在它可以在你没有改变输入文件的任何内容的情况下工作

import json

class STR_TO_JSON():
    def __init__(self, filename):
        self.filename=str(filename)
        self.read_scan_output()
        self.edit_json_structure()

    #read the file and save the str on self.output_file
    def read_scan_output (self):
        f = open(self.filename,"r") 
        self.output_file= (f.read())
        f.close()

    #take your string and format it
    def edit_json_structure (self):
        # if in your string is present "} {" or "}{ I replace it with },{ so we divide multiple dictionary with the ,
        if "} {" in self.output_file:
            self.output_file=self.output_file.replace("} {", "},{")
        elif "}{" in self.output_file:
            self.output_file=self.output_file.replace("}{", "},{")
        #seeing that they are multiple dictionary they have to be included in an array/list so I introduce at the beginning and at the end of your string the parentesis []
        self.output_file="["+self.output_file+"]"

    #take the string, convert in json and return the json
    def create_json (self):
        json_output=json.loads(self.output_file)
        return (json_output)

read_file=STR_TO_JSON('your_file.txt')
json_dict=read_file.create_json()

答案 3 :(得分:0)

其中s=<your string>

for x in re.findall('(?:\s*)({.*?})', s):
    print(json.loads(x))

对我来说效果很好:))

{'__CURSOR': 's=b0110bcb6bd44e8394728027cb5d82ce;i=b06;b=8ee885ed17a141abb35b8ef4903e8a41;m=158e9e2920;t=569a4d5ac3942;x=d80562ac11b34953', '__REALTIME_TIMESTAMP': '1523531563940162', '__MONOTONIC_TIMESTAMP': '92587043104', '_BOOT_ID': '8ee885ed17a141abb35b8ef4903e8a41', 'SYSLOG_FACILITY': '3', 'PRIORITY': '6', '_UID': '0', '_GID': '0', '_MACHINE_ID': '07c42c0296d34aea8b37c0acb2c260a8', 'SYSLOG_IDENTIFIER': 'systemd', '_TRANSPORT': 'journal', 'CODE_FILE': '../git/src/core/job.c', 'CODE_LINE': '803', 'CODE_FUNCTION': 'job_log_status_message', 'MESSAGE_ID': '39f53479d3a045ac8e11786248231fbf', 'RESULT': 'done', '_PID': '1', '_COMM': 'systemd', '_EXE': '/lib/systemd/systemd', '_CMDLINE': '/sbin/init', '_CAP_EFFECTIVE': '3fffffffff', '_SYSTEMD_CGROUP': '/init.scope', '_SYSTEMD_UNIT': 'init.scope', '_SYSTEMD_SLICE': '-.slice', '_HOSTNAME': 'tait-uvh-7381982', 'UNIT': 'audiorotator.service', 'MESSAGE': 'Started Rotates audio files if they have filled up above the high tide mark..', '_SOURCE_REALTIME_TIMESTAMP': '1523531563938118'}
{...}
{...}