简体   繁体   English

Python:将文本文件解析为格式化的 json 文件

[英]Python: Parse text file to a formatted json file

I have a text file and need to extract some information and write it into a JSON file in a formatted way.我有一个文本文件,需要提取一些信息并以格式化的方式将其写入 JSON 文件。

Text file:文本文件:

Jul 23 06:43:06 localhost : [file.download][Informational][0X1013] Attempting connection to https://fileserver/file/abcdefg
Jul 23 06:43:06 localhost : [file.download][Informational][0X800F0000] CA file error

Required JSON formatted way:所需 JSON 格式化方式:

{'Timestamp': 'Jul 23 06:43:06 ', 'Source': 'file.download', 'Level': 'Informational', 'code': '0X1013', 'messages': 'Attempting connection to https://fileserver/file/abcdefg'}
{'Timestamp': 'Jul 23 06:43:06 ', 'Source': 'file.download', 'Level': 'Informational', 'code': '0X800F0000', 'messages': 'CA file error'}

Code:代码:

import json

with open('c:\Temp\log.txt', 'r') as data:
    result = [ {
            'Timestamp': line.strip().split('localhost : ')[0],
            'Source': line.strip().split('[')[1].rstrip(']'),
            'Level': line.strip().split('[')[2].rstrip(']'),
            'code': line.strip().split('[')[3].split(']')[0],
            'messages': line.strip().split('[')[3].split(']')[1].strip()
            } for line in data]

print(result)

with open('output.json', 'w') as json_file:
    json_file.write(json.dumps(result))

Output: Output:

[{'Timestamp': 'Jul 23 06:43:06 ', 'Source': 'file.download', 'Level': 'Informational', 'code': '0X1013', 'messages': 'Attempting connection to https://fileserver/file/abcdefg'}, {'Timestamp': 'Jul 23 06:43:06 ', 'Source': 'file.download', 'Level': 'Informational', 'code': '0X800F0000', 'messages': 'CA file error'}]

Questions:问题:

What must I do in order to make code output to be the same as the "Required JSON formatted way"?我必须做什么才能使代码 output 与“所需的 JSON 格式化方式”相同?

think this works (help from Dump two dictionaries in a json file on separate lines )认为这行得通(来自Dump two dictionary in a json file on separate lines 的帮助)

import json

with open('c:\Temp\log.txt', 'r') as data:
    result = [ {
            'Timestamp': line.strip().split('localhost : ')[0],
            'Source': line.strip().split('[')[1].rstrip(']'),
            'Level': line.strip().split('[')[2].rstrip(']'),
            'code': line.strip().split('[')[3].split(']')[0],
            'messages': line.strip().split('[')[3].split(']')[1].strip()
            } for line in data]


with open('output.json', 'w') as json_file:

    json.dump(result[0], json_file)
    json_file.write('\n')
    json.dump(result[1], json_file)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM