38 lines
1.2 KiB
Python
38 lines
1.2 KiB
Python
|
def process_file(input_file_path, output_file_path):
|
|||
|
try:
|
|||
|
# 打开输入文件进行读取
|
|||
|
with open(input_file_path, 'r', encoding='utf-8') as input_file:
|
|||
|
lines = input_file.readlines()
|
|||
|
|
|||
|
processed_lines = []
|
|||
|
empty_line_count = 0
|
|||
|
|
|||
|
for line in lines:
|
|||
|
if line.strip() == '':
|
|||
|
# 如果当前行为空行
|
|||
|
empty_line_count += 1
|
|||
|
if empty_line_count <= 2:
|
|||
|
# 若空行数量不超过 2,则添加该空行
|
|||
|
processed_lines.append(line)
|
|||
|
else:
|
|||
|
# 如果当前行不为空行
|
|||
|
processed_lines.append(line)
|
|||
|
empty_line_count = 0
|
|||
|
|
|||
|
# 打开输出文件进行写入
|
|||
|
with open(output_file_path, 'w', encoding='utf-8') as output_file:
|
|||
|
output_file.writelines(processed_lines)
|
|||
|
|
|||
|
print(f"处理完成,结果已保存到 {output_file_path}")
|
|||
|
|
|||
|
except FileNotFoundError:
|
|||
|
print(f"未找到输入文件: {input_file_path}")
|
|||
|
|
|||
|
# 输入文件路径
|
|||
|
input_file = 'a00006.txt-merge.txt-new.txt'
|
|||
|
# 输出文件路径
|
|||
|
output_file = 'output.txt'
|
|||
|
|
|||
|
process_file(input_file, output_file)
|
|||
|
|