Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:将文本文件解析为格式化的json文件_Python_Json_Parsing - Fatal编程技术网

Python:将文本文件解析为格式化的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

我有一个文本文件,需要提取一些信息并以格式化的方式将其写入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'}
所需的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
文件的示例非常感谢您的时间。我设法得到格式化的行。