Python:将文本文件解析为格式化的json文件
我有一个文本文件,需要提取一些信息并以格式化的方式将其写入JSON文件 文本文件:Python:将文本文件解析为格式化的json文件,python,json,parsing,Python,Json,Parsing,我有一个文本文件,需要提取一些信息并以格式化的方式将其写入JSON文件 文本文件: 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
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
{'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'}
所需的JSON格式方式:
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
{'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'}
代码:
输出:
[{'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'}]
问题:
为了使代码输出与“必需的JSON格式方式”相同,我必须做些什么
认为这有效(来自的帮助)
需要更改什么?请提供
.txt
文件的示例非常感谢您的时间。我设法获得格式化的行。需要更改什么?请提供.txt
文件的示例非常感谢您的时间。我设法得到格式化的行。