gpt4 book ai didi

python - 在python中解析日志文件

转载 作者:行者123 更新时间:2023-11-28 16:51:24 25 4
gpt4 key购买 nike

我有一个日志文件,其中包含如下行:

"1","2546857-23541","f_last","user","4:19 P.M.","11/02/2009","START","27","27","3","c2546857-23541",""

日志中的每一行都是 12 个双引号部分,字符串中的第 7 个双引号部分来自用户在聊天窗口中键入内容的位置:

"22","2546857-23541","f_last","john","4:38 P.M.","11/02/2009","
What's up","245","47","1","c2546857-23541",""

这个字符串也显示了我遇到的问题;在聊天日志的某些区域,用户键入的文本位于日志文件的新行中,而不是像第一个示例那样位于同一行。所以基本上我希望第二个示例中的行看起来像第一个示例。

我试过在 N++ 中使用 Find/Replace,我能够找到每个“孤立”的行,但我无法让它加入它上面的行。然后我想制作一个 python 文件来为我自动执行它,但我对如何实际编码感到困惑。


在运行 unutbu 代码的这一行出现 Python 错误

"1760","4746880-00129","bwhiteside","tom","11:47 A.M.","12/10/2009","I do not see ^"refresh your knowledge
^" on the screen","422","0","0","c4746871-00128",""

最佳答案

csv module足够聪明,可以识别引用的项目何时未完成(因此必须包含换行符)。

import csv
with open('data.log',"r") as fin:
with open('data2.log','w') as fout:
reader=csv.reader(fin,delimiter=',', quotechar='"', escapechar='^')
writer=csv.writer(fout, delimiter=',',
doublequote=False, quoting=csv.QUOTE_ALL)
for row in reader:
row[6]=row[6].replace('\n',' ')
writer.writerow(row)

关于python - 在python中解析日志文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6930874/

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