例如,我有一个包含很多行的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中。
我该怎么办? 谢谢!
答案 0 :(得分: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。
如果您是正则表达式的新手,此网站对于调试和测试匹配项非常有用:https://regex101.com/
答案 1 :(得分: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()