将日志文件转换为json吗? [英] Converting log file to json?
问题描述
我具有以下格式的以下日志文件.我需要使用python将日志文件转换为json文件.怎么做?
I have the below log file in the following format. I need to convert the log file into json file using python. How can it be made?
[2015-07-13 00:03:05,976] hostname 1499918592344 UZA:Anonymous:Anonymous96B50456767E74F51FD6AD2730C24133 http-nio-8080-exec-61 INFO Got successful response for the url GET http:/hostname/uza/accounts/123456789?loginTime=2017-07-13T00:03:04EDT
Response: {"accountBalance":{"pointsBalance":95053,"pointsName":"dd"},"accountStatus":{"accessType":"STANDARD","statusCode":"ACTIVE","statusMessage":"Unknown"},"userInformation":{"additionalInfo":{"memberID":"dd","updatedMemberID":"dd","memberLevel":"0"},"address":{"line1":"10249 dd","city":"dd Park","stateCode":"vv","postalCode":"777","countryCode":"rr"},"emailAddresses":[{"email":"dd@YAHOO.COM","type":"OTHER"}],"firstName":"gg","lastName":"gg","middleName":"C","phoneNumbers":[{"number":"5555","type":"OTHER"}],"title":"Mr"},"pricingTier":"ggg"} (HttpClientUtil)
[2015-07-13 00:03:05,976] hostname 1499918592344 UZA:Anonymous:Anonymous96B50456767E74F51FD6AD2730C24133 http-nio-8080-exec-61 INFO Got successful response for the url GET http:/hostname/uza/accounts/123456789?loginTime=2017-07-13T00:03:04EDT
Response: {"accountBalance":{"pointsBalance":95053,"pointsName":"dd"},"accountStatus":{"accessType":"STANDARD","statusCode":"ACTIVE","statusMessage":"Unknown"},"userInformation":{"additionalInfo":{"memberID":"dd","updatedMemberID":"dd","memberLevel":"0"},"address":{"line1":"10249 dd","city":"dd Park","stateCode":"vv","postalCode":"777","countryCode":"rr"},"emailAddresses":[{"email":"dd@YAHOO.COM","type":"OTHER"}],"firstName":"gg","lastName":"gg","middleName":"C","phoneNumbers":[{"number":"5555","type":"OTHER"}],"title":"Mr"},"pricingTier":"ggg"} (HttpClientUtil)
推荐答案
导入 pythons json库:
import json
以字符串形式读取文件,并获取"Response:"子字符串之后的所有内容:
Read in the file as a string and get everything after the 'Response:' substring:
with open("logfile", "r") as log_file:
log_string = log_file.read()
response_string = log_string.split("Response:")[1].strip()
从response_string
获取python对象:
Get a python object from response_string
:
response_obj = json.loads(response_string)
如果需要,可以在对文件进行任何操作后将该对象写到文件中:
If you need to, write that object out to a file after doing whatever you need with it:
with open("outfile", "w") as out_file:
out_file.write(json.dumps(response_obj))
这篇关于将日志文件转换为json吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!