简体   繁体   中英

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.

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

Make it look like following:

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

2018-11-28 13:12:01.023 starting test

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 ").

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.

There has to be some pattern to make this happen. Given that all the logs are from 2018, you could do something like

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. 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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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