gpt4 book ai didi

python - 如何打开文本文件中的包装线,重新格式化文本文件

转载 作者:行者123 更新时间:2023-11-28 19:27:10 25 4
gpt4 key购买 nike

我需要帮助找到一个 Python 解决方案来重新格式化换行/重写日志文件,这样就没有所描述的换行符。这将使我能够继续在不间断的线路上寻找。

*.log 中的每个条目都带有时间戳。太长的行按预期换行,但是:换行的部分也带有时间戳。 “>”(大于)是行已换行的唯一指示 - 发生在位置 37。> 日志来自 *nix 机器。

我不知道如何开始......

2011-223-18:31:11.737  VWR:tao       abc exec /home/abcd/abcd9.94/bin/set_specb.tcl -s DL 2242.500000 5
2011-223-18:31:11.737 > -20.000000 10
###needs to be rewritten as:
2011-223-18:31:11.737 VWR:tao abc exec /home/abcd/abcd9.94/bin/set_specb.tcl -s DL 2242.500000 5 -20.000000 10

还有一个

2011-223-17:40:07.039  EVT:703       agc_drift_cal.tcl: out of tolerance drift of 5.3080163871 detected! Downlink Alignmen
2011-223-17:40:07.039 >t check required.
###these lines deleted and consolodated as one:
2011-223-17:40:07.039 EVT:703 agc_drift_cal.tcl: out of tolerance drift of 5.3080163871 detected! Downlink Alignment check required.

我不知道如何开始,除了...

for filename in validfilelist:
logfile = open(filename, 'r')
logfile_list = logfile.readlines()
logfile.close
for line in logfile_list:

最佳答案

#!/usr/bin/python

import re

#2011-223-18:31:11.737 > -20.000000 10
ptn_wrp = re.compile(r"^\d+-\d+-\d+:\d+:\d+.\d+\s+>(.*)$")

validfilelist = ["log1.txt", "log2.txt"]

for filename in validfilelist:
logfile = open(filename, 'r')
logfile_new = open("%s.new" % filename, 'w')
for line in logfile:
line = line.rstrip('\n')
m = ptn_wrp.match(line)
if m:
logfile_new.write(m.group(1))
else:
logfile_new.write("\n")
logfile_new.write(line)
logfile_new.write("\n")
logfile.close()
logfile_new.close()

当该行不是换行时写入新行。唯一的副作用是开头有一个空行。对于日志分析应该不是问题。新文件是处理后的结果。

关于python - 如何打开文本文件中的包装线,重新格式化文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7411457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com