You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复用于插入换行符与|-字符的Python脚本?

Python脚本修复:每4行后插入带|-的换行符

我写了个名为line breaks.py的Python脚本,目标是从第300行开始,每连续4行之后插入一行带|-!的换行,但脚本运行结果完全不符合预期,以下是相关代码、输入输出示例,求修复方案。


原脚本代码

# Set the line number to start processing from
start_line_number = 300

# Set the positions where line breaks need to be inserted
break_positions = [1, 0, 0]

# Set the number of line breaks to insert at each position
num_breaks = 1

# Open the input file for reading and the output file for writing
with open('E:\Downloads\Wikipedia\draft data set.txt', 'r') as input_file, open('final data set.txt', 'w') as output_file:
    # Loop through each line in the input file
    for i, line in enumerate(input_file):
        # Write non-modified lines to the output file
        if i < start_line_number or (i - start_line_number) % 4 != 0:
            output_file.write(line)
            continue

        # Insert the desired number of line breaks at each position
        new_lines = []
        for j, pos in enumerate(break_positions):
            new_line = "|-" + line[:pos] + '\n' * num_breaks + "|-" + line[pos:]
            new_lines.append(new_line)

        # Write the modified lines to the output file
        output_file.write('\n'.join(new_lines))

输入示例

! 2020-10-20
| 6177159
| mpac3.2 
|
! 2020-10-01
| 6166863
| mpac3.2 
|
! 2020-09-20
| 6160933
| mpac3.2 
|
! 2020-09-01
| 6151185
| mpac3.2 
|
! 2020-08-20
| 6144071
| mpac3.2
|

期望输出

! 2020-10-20
| 6177159
| mpac3.2 
|
|-!
! 2020-10-01
| 6166863
| mpac3.2 
|
|-!
! 2020-09-20
| 6160933
| mpac3.2 
|
|-!
! 2020-09-01
| 6151185
| mpac3.2 
|
|-!
! 2020-08-20
| 6144071
| mpac3.2
|

实际错误输出

! 2020-10-20
|-|
|- 6177159

|-|-| 6177159

|-|-| 6177159
| mpac3.2 
|
|-|-!
|- 2020-10-01

|-|-! 2020-10-01

|-|-! 2020-10-01
| 6166863
| mpac3.2 
|
|-|-|
|--

|-|-|-|

|-|-|-|
! 2020-09-20
| 6160933
| mpac3.2 
|-|-|
|-|

|-|-|

|-|-|
|-!
! 2020-09-01
| 6151185
|-|-|
|- mpac3.2 

|-|-| mpac3.2 

|-|-| mpac3.2 
|
|-!
! 2020-08-20
|-|-|
|- 6144071

|-|-| 6144071

|-|-| 6144071
| mpac3.2
|

问题分析与修复方案

原脚本核心问题

  1. 逻辑方向错误:把「在4行后插入新行」当成了「修改第4行本身」,反而破坏了原行内容
  2. 冗余代码干扰:break_positions和循环生成new_lines完全多余,导致重复生成错误内容
  3. 行号判断错误:(i - start_line_number) %4 !=0的条件无法准确识别每4行的结束位置
  4. 路径转义问题:Windows路径中的\是转义符,会导致路径解析错误

修复后的脚本

# 开始处理的起始行号
start_line_number = 300

# 打开输入输出文件(用原始字符串避免路径转义问题)
with open(r'E:\Downloads\Wikipedia\draft data set.txt', 'r', encoding='utf-8') as input_file, \
     open('final data set.txt', 'w', encoding='utf-8') as output_file:
    
    for i, line in enumerate(input_file):
        # 先写入当前行
        output_file.write(line)
        
        # 从起始行开始,每4行结束后插入|-!
        if i >= start_line_number and (i - start_line_number + 1) % 4 == 0:
            output_file.write("|-!\n")

修复说明

  1. 去掉所有冗余的break_positions、num_breaks及相关循环,简化逻辑
  2. 调整判断逻辑:先写入原行,再判断是否是每4行的结束位置(从起始行开始计数),是则插入|-!行
  3. 用原始字符串r'路径'解决Windows路径的转义问题,同时指定编码避免乱码
  4. 逻辑更直观:每处理完符合条件的4行,就追加一行|-!,完全匹配期望输出

内容的提问来源于stack exchange,提问作者CrafterNova

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 21:47:15