在写入文件时,尝试跳过与regex匹配的行,但新文件有额外的新行



这是我在这里问的一个问题的衍生产品。

我正在尝试设置一个可以根据输入字典编辑文本文件的方法。这就是我目前所拥有的:

info = {'#check here 1':{'action':'read'}, '#check here 2':{'action':'delete'}}
search_pattern = re.compile(r'.*(#.+)')        
with open(input_file_name, "r") as old_file, open(output_file_name, "w+") as new_file:
lines = old_file.readlines()
for line in lines:
edit_point = search_pattern.search(line)
if edit_point:
result = edit_point.group(1)
if result in info and info[result]["action"] == "insert":#insert new lines to file
print("insert information to file")
new_file.write("n".join([str(n) for n in info[result]["new_lines"]]))
new_file.write(result)
elif result in info and info[result]["action"] == "delete":#skip lines with delete action
print("found deletion point. skipping line")
else:#write to file any line with a comment that is not in info
new_file.write(line)
else:#write lines that do not match regex for (#.*)
new_file.write(line)

基本上,当您提交字典时,程序会遍历文件,搜索注释。如果注释在字典中,它将检查相应的操作。如果操作是插入,则会将行写入文件。如果它被删除,它将跳过那一行。任何没有注释的行都应该写入到新文件中。

我的问题是,当我从文件中删除一行时,它们原来所在的位置似乎有额外的新行。例如,如果我有一个列表:

hello world
how are you #keep this
I'm fine #check here 2
whats up

我预计输出为:

hello world
how are you #keep this
whats up

但我有一行空白:

hello world
how are you #check here 2
whats up

我怀疑这是我的最后一个else语句,它将任何与edit_point不匹配的行写入文件,在本例中为新行。然而,我的理解是for循环应该一行接一行地进行,并且只进行该行。有人能告诉我我在这里缺了什么吗?

这看起来有点混乱,你将读写逻辑与处理逻辑混合在一起,这使得很难跟踪正在发生的事情。请尝试这种方法:

from enum import Enum
from typing import Dict, List

class Action(Enum):
KEEP = "keep"
REMOVE = "remove"

definition = {
"#KEEP": {"action": Action.KEEP},
"#REMOVE": {"action": Action.REMOVE},
}

def clean_comments(
lines: List[str], definition: Dict[str, Dict[str, str]]
) -> List[str]:
# Keep a list of the lines that should be in the output
output: List[str] = []
# Loop the lines
for line in lines:
# If any of the comments in the definition is found, process further
if any([comment in line for comment in definition.keys()]):
# Figure out what to do
for comment, details in definition.items():
if comment in line:
if details["action"] == Action.KEEP:
output.append(line)
break
elif details["action"] == Action.REMOVE:
break
# Keep all other lines
else:
output.append(line)
return output

# Your data here...
with open("test_input.txt", "r") as f:
lines = f.readlines()
# Use the function to clean the text
clean_text = "".join(clean_comments(lines, definition))
# Show the output
print(clean_text)
# Write to file
with open("test.txt", "w") as f:
f.write(clean_text)

输出:

hello world
how are you #KEEP: This line will be kept in the output file
whats up

最新更新