簡體   English   中英

從 Python 的日志文件中提取特定的 JSON

[英]Extract specific JSON from log file in Python

我正在嘗試從包含多個 JSON 和普通文本的日志文件中提取特定的 JSON,在這種情況下,我試圖提取包含“輸出有效負載”文本的 JSON。 我嘗試了多種方法,但無法提取所需的 JSON,文件格式為:

[2020-05-17 15:32:11.698000] INFO [worker-1] org.mule.api.processor.LoggerMessageProcessor [[cloudhub-us-claim-services-1-0-0-prod].post:/claims/{claimNumber}/predictionScores:experience-claims-predictionscore-api.config.7.771]: PredictionScoreAPILogger-7c506940-987d-11ea-9ef4-0a5226a8e24f:16634746: Initialization: Request successfully logged to mirror queue
[2020-05-17 15:32:12.190000] INFO [worker-1] org.mule.transformer.simple.MessagePropertiesTransformer [[cloudhub-us-claim-services-1-0-0-prod].experience-claims-predictionscore-api.prediction-details-claim-updates.stage1.839]: Property with key 'response', not found on message using 'null'. Since the value was marked optional, nothing was set on the message for this property
[2020-05-17 15:32:12.192000] DEBUG [worker-1] aiml.logging.debug [[cloudhub-us-claim-services-1-0-0-prod].experience-claims-predictionscore-api.prediction-details-claim-updates.stage1.839]: PredictionScoreAPILogger-7c506940-987d-11ea-9ef4-0a5226a8e24f:16634746:Datarobot API Call: Output payload received from Datarobot API: {
  "prediction": "N",
  "predictionScore": 0.0000629713,
  "predictionExplanations": "lineItem : 0|feature: ADJER_CANNOT_COMPUTE_TWG_SUGGESTED_TIME_ZERO|Value: Y|strength: -1.4469371757,\nlineItem : 1|feature: ADJER_CANNOT_COMPUTE_TWG_SUGGESTED_PRICE|Value: Y|strength: -1.1968554807,\nlineItem : 2|feature: MONTHS_DIFF_CLAIM_REPAIR_FACILITY_FIRST_CLAIM|Value: 61|strength: -1.0681064444"
}

您可能可以將文件作為文本讀取,然后使用正則表達式對其進行解析。 像這樣的東西:

import re

logfile = open(logfilepath, 'r')
log = logfile.read()
logfile.close()
objects = re.findall("(Output payload.*:\s?)(\{\s?[\s\S]+?\s?\})", log)

我已經為您給定的樣本測試了正則表達式,它工作正常。 所以這段代碼也應該可以工作。 獲得所有 JSON 對象后,您可以輕松找到所需的對象。

快樂的黑客攻擊:)

編輯:根據修改后的問題修改了正則表達式。 正則表達式現在查找“輸出有效負載”字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM