在Python中的某些行之后仅打印行

问题描述 投票:1回答:2

例如,我有一个包含许多行的csv文件

This is line 1
This is line 2 
This is line 3 
This is line 4
This is line 5
This is line 6
This is line 7
This is line 8
This is line 9

使用Python中的代码我只需要打印某些行之后的行,更具体地说,我需要打印第3行之后的行和第7行之后的行,并且在打印之后需要将它们放入另一个csv。

我该怎么做?谢谢!!

python csv web-scraping
2个回答
1
投票

如果你可以合理地预测你的行可能含有什么,使用正则表达式将是我的解决方案。

import re

re_pattern = re.compile(r"This is line [37]")
# The above is used to match "This is line " exactly, followed by either a 3 or a 7.
# The r before the quotations mean the following string should be interpreted literally.

output_to_new_csv = []
print_following_line = False
for line in csv_lines:
    if print_following_line:
        print(line)
        output_to_new_csv.append(line)
    print_following_line = False
    if re.match(re_pattern, line):
        print_following_line = True

# Then write output to your new CSV

代码最初将print_following_line设置为False,因为您不知道是否要打印下一行。如果您的正则表达式字符串与当前行匹配,则print_following_line bool将设置为True。然后它将打印下一行并将其添加到您可以稍后写入CSV的输出列表中。

如果您是regex的新手,这个网站对调试和测试匹配非常有用:https://regex101.com/


1
投票

您可以循环浏览文件中的行,如果找到匹配则返回。像这样的东西:

def find_line_after(target):
    with open('lines.csv', 'r') as f:
        line = f.readline().strip()
        while line:
            if line == target:
                return f.readline().strip()
            line = f.readline().strip()
© www.soinside.com 2019 - 2024. All rights reserved.