[英]How to append dictionary to a json file one by one?
我有以下代碼,它在每次迭代中生成一個 json 對象。 我想在每次迭代時將這些對象追加到 json 文件的新行中,而不是通過制作字典列表然后將其轉換為 json。
def generateDicts(log_fh):
m = 1
currentDict = {}
for line in log_fh:
if line.startswith(matchDate(line)):
if currentDict:
yield currentDict
try:
regexp = re.match(r'(\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d.\d\d\dZ)\s+(INFO|WARN|DEBUG|ERROR|FATAL|TRACE)\s+(.*?\s+.*?)(.*?\-\s+.*?)\s+(?:(\[?.*?\])?)(.*)', line)
currentDict = {
"@timestamp" : regexp.group(1),
"Severity" : regexp.group(2),
"Thread" : regexp.group(3),
"Class" : regexp.group(4),
"Component" : regexp.group(5),
"Message" : regexp.group(6),
"id's" : re.findall(r'\b[a-f0-9]+(?:-[a-f0-9]+)+\b', regexp.group(6))
}
except:
m = m + 1
print(m)
print(line)
else:
currentDict["Message"] += line
yield currentDict
文件創建-
[
{
"@timestamp":"2019-11-14T04:23:08.362Z",
"Severity":"INFO",
"Thread":"ActivityEventRecovery-1 ",
"Class":"ActivityCacheManager -
-",
"Component":"[nsx@6876 comp=\"nsx-manager\" level=\"INFO\" subcomp=\"manager\"]",
"Message":" Handling activity
c85ce68a-1729-4f06-8eb9-4dadd1a59f76 completion event for 1",
"id's":[
"c85ce68a-1729-4f06-8eb9-4dadd1a59f76"
]
},
{
"@timestamp":"2019-11-14T04:25:00.123Z",
"Severity":"WARN",
"Thread":"http-nio-127.0.0.1-7440-exec-127 ",
"Class":"CorfuCompileProxy -
accessInner:",
"Component":null,
"Message":"Encountered a trim
exception while accessing version 120383907 on attempt 0",
"id's":[
]
},
{
"@timestamp":"2019-11-14T04:23:08.700Z",
"Severity":"INFO",
"Thread":"RpcManagerRequestCleanupTimer ",
"Class":"RpcManager -
SYSTEM",
"Component":"[nsx@6876 comp=\"nsx-manager\" level=\"INFO\"
subcomp=\"manager\"]",
"Message":" Rpc response not received for
application FabricStats request
com.vmware.nsx.management.agg.messaging.AggService$ClientDataRequestMsg
from client 8ac94189-d611-4eb3-9b93-c3c3a8e3d36a with correlation id
287e690e-0a47-4459-a0bb-be36fe439068 in 432000000 msec.",
"id's":[
"8ac94189-d611-4eb3-9b93-c3c3a8e3d36a",
"287e690e-0a47-4459-a0bb-be36fe439068"
]
},
{
"@timestamp":"2019-11-14T04:24:04.072Z",
"Severity":"INFO",
"Thread":"MessagingObjectFactoryImpl-4-2 ",
"Class":"ExporterLastAckServiceImpl - -",
"Component":"[nsx@6876
comp=\"nsx-manager\" level=\"INFO\" subcomp=\"manager\"]",
"Message":" Found exporter with elaId =
Node#a3844284-e626-11e9-a87b-005056bcc0c6#AggSvc-L2-Bridging,
returning lastAck = 16507 ",
"id's":[
"a3844284-e626-11e9-a87b-005056bcc0c6"
]
},
{
"@timestamp":"2019-11-14T04:23:08.362Z",
"Severity":"INFO",
"Thread":"ActivityEventRecovery-1 ",
"Class":"ActivityCacheManager - -",
"Component":"[nsx@6876 comp=\"nsx-manager\" level=\"INFO\"
subcomp=\"manager\"]",
"Message":" Handling activity
92d6a146-fa12-4889-a0ff-441087e047d0 completion event for 1 ",
"id's":[
"92d6a146-fa12-4889-a0ff-441087e047d0"
]
},
{
"@timestamp":"2019-11-14T04:23:08.362Z",
"Severity":"DEBUG",
"Thread":"ActivityEventRecovery-1 ",
"Class":"ActivityCacheManager - -",
"Component":"[nsx@6876 comp=\"nsx-manager\" level=\"INFO\"
subcomp=\"manager\"]",
"Message":" Handling activity
92d6a146-fa12-4889-a0ff-441087e047d0 completion event for 1\n",
"id's":[
"92d6a146-fa12-4889-a0ff-441087e047d0"
]
}
]
我要的文件-
{"@timestamp": "2019-11-14T04:25:00.123Z", "Severity": "WARN", "Thread": "http-nio-127.0.0.1-7440-exec-127 ", "Class": "CorfuCompileProxy - accessInner:", "Component": null, "Message": "Encountered a trim exception while accessing version 120383907 on attempt 0", "id's": []}
{"@timestamp": "2019-11-14T04:23:08.700Z", "Severity": "INFO", "Thread": "RpcManagerRequestCleanupTimer ", "Class": "RpcManager - SYSTEM", "Component": "[nsx@6876 comp=\"nsx-manager\" level=\"INFO\" subcomp=\"manager\"]", "Message": " Rpc response not received for application FabricStats request com.vmware.nsx.management.agg.messaging.AggService$ClientDataRequestMsg from client 8ac94189-d611-4eb3-9b93-c3c3a8e3d36a with correlation id 287e690e-0a47-4459-a0bb-be36fe439068 in 432000000 msec.", "id's": ["8ac94189-d611-4eb3-9b93-c3c3a8e3d36a", "287e690e-0a47-4459-a0bb-be36fe439068"]}
{"@timestamp": "2019-11-14T04:24:04.072Z", "Severity": "INFO", "Thread": "MessagingObjectFactoryImpl-4-2 ", "Class": "ExporterLastAckServiceImpl - -", "Component": "[nsx@6876 comp=\"nsx-manager\" level=\"INFO\" subcomp=\"manager\"]", "Message": " Found exporter with elaId = Node#a3844284-e626-11e9-a87b-005056bcc0c6#AggSvc-L2-Bridging, returning lastAck = 16507 ", "id's": ["a3844284-e626-11e9-a87b-005056bcc0c6"]}
{"@timestamp": "2019-11-14T04:23:08.362Z", "Severity": "INFO", "Thread": "ActivityEventRecovery-1 ", "Class": "ActivityCacheManager - -", "Component": "[nsx@6876 comp=\"nsx-manager\" level=\"INFO\" subcomp=\"manager\"]", "Message": " Handling activity 92d6a146-fa12-4889-a0ff-441087e047d0 completion event for 1 ", "id's": ["92d6a146-fa12-4889-a0ff-441087e047d0"]}
{"@timestamp": "2019-11-14T04:23:08.362Z", "Severity": "DEBUG", "Thread": "ActivityEventRecovery-1 ", "Class": "ActivityCacheManager - -", "Component": "[nsx@6876 comp=\"nsx-manager\" level=\"INFO\" subcomp=\"manager\"]", "Message": " Handling activity 92d6a146-fa12-4889-a0ff-441087e047d0 completion event for 1\n", "id's": ["92d6a146-fa12-4889-a0ff-441087e047d0"]}```
import json
with open(FileName, "a") as outjs:
c=[{"@timestamp": "2019-11-14T04:25:00.123Z", "Severity": "WARN"},{ "Thread": "http-nio-127.0.0.1-7440-exec-127 ", "Class": "CorfuCompileProxy - accessInner:", "Component": None, "Message": "Encountered a trim exception while accessing version 120383907 on attempt 0", "id's": []}
]
for currentDict in c:
json.dump(currentDict, outjs) # write the dictionary
outjs.write('\n')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.