Python实现:当下一行以特定字符开头时删除当前行
Hey there! Since you're new to Python, let's break this down simply. The core idea here is to check each line against the next line in the file—if the next line starts with "-----a", we skip the current line; otherwise, we keep it. Here are two straightforward approaches to implement this:
方法1:一次性读取所有行(适合小文件)
This approach reads all lines into a list first, which makes it easy to check the next line using indexes. Perfect for smaller files where memory isn't an issue.
# 读取文件所有行 with open('your_file.txt', 'r') as input_file: lines = input_file.readlines() processed_lines = [] # 遍历每一行,注意要留最后一行单独处理 for idx in range(len(lines)): # 不是最后一行的话,检查下一行的开头 if idx < len(lines) - 1: next_line = lines[idx + 1] # 如果下一行以"-----a"开头,跳过当前行 if next_line.startswith("-----a"): continue # 保留当前行 processed_lines.append(lines[idx]) # 将处理后的内容写回文件 with open('your_file.txt', 'w') as output_file: output_file.writelines(processed_lines)
代码解释:
with open(...):安全地读写文件,不用手动关闭文件句柄。readlines():把文件内容按行读取到一个列表里,每个元素是一行文本。- 遍历的时候,对于每一行(除了最后一行),我们检查下一行是否以
"-----a"开头。如果是,就跳过当前行;否则把它加入结果列表。 - 最后一行肯定会被保留,因为它没有下一行可以触发删除条件。
方法2:逐行处理(适合大文件)
If your file is very large, loading all lines into memory might not be ideal. This method processes lines one by one, keeping track of the previous line instead:
processed_lines = [] previous_line = None with open('your_file.txt', 'r') as input_file: for current_line in input_file: # 当我们有上一行时,判断是否要保留它 if previous_line is not None: # 如果当前行不是以"-----a"开头,就保留上一行 if not current_line.startswith("-----a"): processed_lines.append(previous_line) # 更新上一行为当前行,继续循环 previous_line = current_line # 最后把最后一行加入结果(没有下一行,所以必须保留) if previous_line is not None: processed_lines.append(previous_line) # 写回文件 with open('your_file.txt', 'w') as output_file: output_file.writelines(processed_lines)
代码解释:
- We keep track of
previous_lineas we iterate through each line. - For every
current_line, we check if it starts with"-----a": if it doesn't, we add theprevious_lineto our result. If it does, we skip adding theprevious_line(which is exactly what we want—delete the line whose next line starts with"-----a"). - After the loop ends, we add the last line to the result since there's no next line to check against.
测试你的示例
If you run either of these scripts on your sample input:
-----arn:aws:iam::001122334455:policy/policy_name1 # 删除此行
-----arn:aws:iam::001122334455:policy/policy_name2 # 删除此行
-----arn:aws:iam::001122334455:policy/policy_name3 # 删除此行
-----arn:aws:iam::001122334455:policy/policy_name4 # 删除此行
-----arn:aws:iam::001122334455:policy/policy_name5 # 请勿删除此行
"Action": "s3:*", # 此行也请勿删除
-----arn:aws:iam::001122334455:policy/policy_name6 # 删除此行
-----arn:aws:iam::001122334455:policy/policy_name7 # 删除此行
The output will be:
-----arn:aws:iam::001122334455:policy/policy_name5 # 请勿删除此行
"Action": "s3:*", # 此行也请勿删除
-----arn:aws:iam::001122334455:policy/policy_name7 # 删除此行
Why is the last line kept? Because it's the final line in the file—there's no next line to trigger the deletion condition, so we keep it as per your rules. If you intended to delete standalone -----a lines too, we can adjust the code, but based on your original description, this matches your requirements.
内容的提问来源于stack exchange,提问作者Naxxio

