简体   繁体   English

使用Python在TXT文件中的每个时间戳之前添加换行符

[英]Adding a line break before each time stamp in TXT file using Python

I am trying to make a TXT log file more readable by adding a line break before each time stamp, using python script. 我试图通过使用python脚本在每个时间戳之前添加换行符来使TXT日志文件更具可读性。

current file sample: 2018-11-28 13:12:01.023 parameter definition!2018-11-28 13:12:01.023 starting test 当前文件样本:2018-11-28 13:12:01.023参数定义!2018-11-28 13:12:01.023开始测试

Make it look like following: 使其如下所示:

2018-11-28 13:12:01.023 parameter definition! 2018-11-28 13:12:01.023参数定义!

2018-11-28 13:12:01.023 starting test 2018-11-28 13:12:01.023开始测试

If you know that your lines are always going to start with a timestamp in the form of "XXXX-XX-XX XX:XX:XX.XXX", where each "X" is a numerical digit, then you can use a regular expression to identify any occurrences, then stick a newline character (" \\n ") before any of the matches it finds (these matches are symbolized by " \\1 "). 如果您知道行总是以“ XXXX-XX-XX XX:XX:XX.XXX”形式的时间戳开头,其中每个“ X”都是数字,那么可以使用正则表达式要识别任何出现的内容,请在找到的任何匹配项之前插入换行符(“ \\n ”)(这些匹配项由“ \\1 ”表示)。

import re

log = r"2018-11-28 13:12:01.023 parameter definition!2018-11-28 13:12:01.023 starting test1955-10-15 01:22:33.987 hi"

pattern = r'(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3})'
replacement_string = r'\n\1'
print(re.sub(pattern, replacement_string, log))

Output: 输出:

2018-11-28 13:12:01.023 parameter definition!
2018-11-28 13:12:01.023 starting test
1955-10-15 01:22:33.987 hi

Note that this invariably sticks a newline character at the very beginning of your string, even if there's no line on top of it to separate from. 请注意,即使在字符串的开头没有分隔线,这也总是在字符串的开头插入换行符。

Though as Rasthiya showed, if you know the timestamps are just going to start with "2018", and that you don't expect "2018" to show up anywhere else except for the start of a timestamp, then that simple substitution may be enough. 尽管正如Rasthiya所示,但如果您知道时间戳只是以“ 2018”开头,并且您不希望“ 2018”出现在除时间戳开头之外的其他任何地方,那么简单的替换就足够了。

There has to be some pattern to make this happen. 必须有某种模式才能实现这一目标。 Given that all the logs are from 2018, you could do something like 鉴于所有日志均来自2018年,您可以执行以下操作

string = "2018-11-28 13:12:01.023 parameter definition!2018-11-28 13:12:01.023 starting test"
print(string.replace("2018", "\n2018"))

which gives 这使

2018-11-28 13:12:01.023 parameter definition!
2018-11-28 13:12:01.023 starting test

You could do the same thing with a text editor like notepad++ as well. 您也可以使用文本编辑器(如notepad ++)执行相同的操作。 Just search for the pattern and replace it with the new line character and pattern. 只需搜索图案,然后用新的换行符和图案替换它即可。 For example, search for "2018" and replace it with "\\n2018" 例如,搜索“ 2018”并将其替换为“ \\ n2018”

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

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