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

时间:2015-02-24 17:18:07

标签: python csv

我有一个看起来像这样的文本文件:

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

我想提取特定的行(示例中的 item )并将它们保存为具有以下格式的csv文件:

First item A | Second item A | Third item A

First item B | Second item B | Third item B

其中|意思是单独的列。

这是我在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)

下一步我认为我可以在每个“第三项”之后拆分列表,但我开始怀疑有更简单的方法可以解决这个问题。

1 个答案:

答案 0 :(得分:0)

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