从fi中删除换行符

2024-06-25 07:27:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个以下格式的文本文件:

Run#1 Step#1 > Connecting to server
Run#1 Step#2 > Connected OK
Run#1 Step#3 > Sending request: {
    "path": "/testpage",
    "time": "2015-06-07T00:00:00.000Z"
}
Run#1 Step#4 > Request sent OK

我需要做的是处理这个文件。如果每个步骤都打印在单独的行上,会更容易:

Run#1 Step#1 > Connecting to server
Run#1 Step#2 > Connected OK
Run#1 Step#3 > Sending request: { "path": "/testpage", "time": "2015-06-07T00:00:00.000Z" }
Run#1 Step#4 > Request sent OK

我如何做到这一点(在bash或ruby/python/。。。脚本)?你知道吗


Tags: topathrunservertimerequest格式step
3条回答

1)拆分(“\n”) 2) 替换(“运行#”,“\n运行#”) 3) 删除第一行(“\n”)

gnu-sed解决方案

cat file | sed ':a; N; $! ba; s/\n//g; s/Run#/\nRun#/g;' | sed '1d;' > outputfile

使用python根据以Run#开头的行对行进行分组,并将不以Run#开头的行的任何部分连接到上一个Run#行(无论内容如何),它还将替换原始文件,您不需要将整个文件读入内存:

from itertools import groupby
from tempfile import NamedTemporaryFile
from shutil import move

with open("file.txt") as f, NamedTemporaryFile("w",dir=".",delete=False) as  out:
    grouped = groupby(f, key=lambda x: not x.startswith("Run#"))
    for k, v in grouped:
        if not k:
            v, nxt = "".join(v), next(grouped, "  ")[1]
            out.write("{}{}\n".format(v.rstrip(), "".join(map(str.strip, nxt))))
        else:
            out.writelines(v)

move(out.name,"file.txt")

输出:

Run#1 Step#1 > Connecting to server
Run#1 Step#2 > Connected OK
Run#1 Step#3 > Sending request: {"path": "/testpage","time": "2015-06-07T00:00:00.000Z"}
Run#1 Step#4 > Request sent OK

相关问题 更多 >