大家好,我有一个日志文件,这个日志信息是我通过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)我不知道为什么它不起作用实际上我不太了解json所以我不知道怎么做如果有人知道怎么做请帮助me..Thanks
发布于 2018-12-05 09:25:26
如果要将日志文件转换为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。
https://stackoverflow.com/questions/53626919
复制相似问题