大家好我有一个日志文件这个日志信息我是由
python程序创建的,但是想把这个日志文件打印成.json文件
2018-12-04 11:45:41,820 - DATA-MANUFACTURING - INFO - Uploading the file
2018-12-04 11:45:41,852 - DATA-MANUFACTURING - DEBUG - recordCount - 1000
2018-12-04 11:45:41,853 - DATA-MANUFACTURING - DEBUG - fromYear - 1970
2018-12-04 11:45:41,853 - DATA-MANUFACTURING - DEBUG - toYear - 1974
2018-12-04 11:45:41,854 - DATA-MANUFACTURING - DEBUG - FileName - <FileStorage: 'Data_Large1.csv' ('application/vnd.ms-excel')>
2018-12-04 11:45:41,899 - DATA-MANUFACTURING - DEBUG - File saved to destination folder
2018-12-04 11:45:41,900 - DATA-MANUFACTURING - INFO - Uploading file is done
2018-12-04 11:45:41,902 - werkzeug - INFO - 127.0.0.1 - - [04/Dec/2018 11:45:41] "POST /UploadFile HTTP/1.1" 302 -
2018-12-04 11:45:41,912 - DATA-MANUFACTURING - INFO - Data manufacturing process started
2018-12-04 11:45:41,913 - DATA-MANUFACTURING - DEBUG - Year Array - [1970, 1971, 1972, 1973, 1974]
2018-12-04 11:45:41,954 - DATA-MANUFACTURING - INFO - Readed the csvfile
2018-12-04 11:45:41,955 - DATA-MANUFACTURING - INFO - Segmentation for the 'recordCount' is started
2018-12-04 11:45:41,955 - DATA-MANUFACTURING - INFO - SegmentValues is appended
2018-12-04 11:45:41,956 - DATA-MANUFACTURING - DEBUG - segmentValues - [1, 2.0, 3, 4, 2, 1, 5, 89, 1, 10, 81, 1]
2018-12-04 11:45:41,957 - DATA-MANUFACTURING - INFO - segmentation for the data is done
2018-12-04 11:45:42,183 - DATA-MANUFACTURING - INFO - Segmentation for the 'recordCount' is started
2018-12-04 11:45:42,184 - DATA-MANUFACTURING - INFO - SegmentValues is appended
2018-12-04 11:45:42,185 - DATA-MANUFACTURING - DEBUG - segmentValues - [1, 1, 9, 108, 31, 1, 35, 1, 1, 1, 2.0, 9]
2018-12-04 11:45:42,186 - DATA-MANUFACTURING - INFO - segmentation for the data is done
2018-12-04 11:45:42,475 - DATA-MANUFACTURING - INFO - Segmentation for the 'recordCount' is started
2018-12-04 11:45:42,476 - DATA-MANUFACTURING - INFO - SegmentValues is appended
2018-12-04 11:45:42,477 - DATA-MANUFACTURING - DEBUG - segmentValues - [1, 1, 8, 1, 5, 1, 112, 2.0, 48, 1, 2, 18]
2018-12-04 11:45:42,477 - DATA-MANUFACTURING - INFO - segmentation for the data is done
为此,我写了一个在线代码:
with open("info.log", "r") as log_file:
log_string = log_file.read().splitlines()
response_string = log_string.split("Response :")[1].strip()
response_obj = json.loads(response_string)
with open("outfile", "w") as out_file:
out_file.write(json.dumps(response_obj))
但在这里我得到的错误如下:
IndexError: list index out of range
然后我尝试了其他方式:
with open('data_generation.log', 'r') as logfile, open('output.json', 'w') as jsonfile:
json_data = re.search(r'(Response:\s*)(.*)(?=\(HttpClientUtil\))', logfile.read(), re.DOTALL)
if json_data:
json.dump(json.loads(json_data.group(2)), jsonfile)
在这里DOTALL不起作用我不知道为什么它不起作用…实际上我不知道json这么多,所以如果有人知道该怎么做我也没有得到它,请帮助我…谢谢
最佳答案 如果要将日志文件转换为json,
你可以逐行阅读日志文件
fo = open(“foo.txt”,“rw”)
linesOfLogs = fo.readlines()
一旦你有了这些行,我们可以遍历linesofLogs并根据’Response:’拆分字符串,然后我们可以使用所有元素作为除最后一个之外的键.并使用最后一个作为json的值
我们可以这样做:
jsonData = {}
for lineOfLog in liesOfLogs:
listOfStrings = lineOfLog.split('Response :')
if len(listOfStrings)>1:
jsonData[listOfStrings[0]] = listOfStrings[1]
现在我们可以使用python的json库轻松保存jsonData