简体   繁体   English

从python中提取特定行并格式化输出

[英]Extract specific lines from python and format the output

I have a text file that looks something like this: 我有一个看起来像这样的文本文件:

Some text first

First item A

Second item A

Third item A


Some more text


First item B

Second item B

Third item B

More text

I want to extract specific rows ( item in the example) and save them as a csv file with the following formatting: 我想提取特定的行(示例中的项目 )并将它们保存为具有以下格式的csv文件:

First item A | Second item A | Third item A

First item B | Second item B | Third item B

where | 哪里| means seperate column. 意思是单独的列。

Here is my attempt in Python: I create a list, open the text file and iterate through it, then append each item that contains the right keywords to my list. 这是我在Python中的尝试:我创建一个列表,打开文本文件并遍历它,然后将包含正确关键字的每个项目附加到我的列表中。

import sys
sys.stdout = open('out.csv', 'w')

f = open("input.txt").readlines()

l = []

for line in f:
    if("First" in line and not "Some text" in line):
        l.append(line.rstrip())

    if("Second" in line):
        l.append(line.rstrip())

    if("Third" in line):
        l.append(line.rstrip())

print(l)

As the next step I was thinking I could split up the list after each "third item" but I am starting to suspect there is an easier way to go about this. 作为下一步,我认为我可以在每个“第三项”之后拆分列表,但我开始怀疑有更简单的方法来解决这个问题。

with open("in.txt") as f:
    out = [[]]
    for line in f:
         # if any line starts with "First","Second" or "Third" append it 
         if any(line.startswith(x) for x in ("First","Second","Third")):
            out[-1].append(line.rstrip())
            # if it starts with Third add a new list for next section
            if line.startswith("Third"):
                out.append([])
for row in out:
    print(" | ".join(row))

First item A | Second item A | Third item A
First item B | Second item B | Third item B

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

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