【问题标题】:Replace multiple lines after pattern in python?在python中的模式后替换多行?
【发布时间】:2016-09-21 14:30:16
【问题描述】:

我想编写一个python脚本来替换我的模式后多行中的第一个单词,现在我只能替换我的模式后的一行,它如何替换更多的行?比方说3行。

lines.txt(输入文件,模式“第 2 节”):

section 1
line 1
line 2
line 3
line 4
endsection
section 2
line 1
line 2
line 3
line 4
endsection
section 3
line 1
line 2
line 3
line 4
endsection

lines_mod.txt(我当前代码的结果):

section 1
line 1
line 2
line 3
line 4
endsection
section 2
mod 1
line 2
line 3
line 4
endsection
section 3
line 1
line 2
line 3
line 4
endsection

这是我的 python 脚本:

with open('E:/lines.txt') as fin, open('E:/lines_m.txt', 'w') as fout:
    flag = 0
    for line in fin:
        if flag == 1:
            mod_line = 'mod ' + line.split()[-1] + '\n'
            fout.write(mod_line)
            flag = 0
            continue
        fout.write(line)
        if line.find('section 2') != -1:
            flag = 1

感谢您的帮助。

【问题讨论】:

  • 是因为你在找到'section 2'之后把你的flag设置为0,然后写一行文件出来。你再也找不到第 2 节了。如果你想继续修改第 2 节的所有内容。不要在写一行之后设置flag = 0,而是在你点击endsection之后设置它
  • 请更新代码的缩进。
  • @MooingRawr:我不想替换“第 2 节”中的所有行,我只想替换其中的一些。
  • @Tian 然后有一个行数计数器
  • @MooingRawr:我对编程和 python 很陌生。你能给我看看柜台的东西吗?谢谢

标签: python python-2.7


【解决方案1】:
list_of_words_to_replace = ['mod','apple','xxx']
with open('E:/lines.txt') as fin, open('E:/lines_mod.txt', 'w') as fout:
    flag = 0
    counter = 0  # <<<<------- added a counter 
    for line in fin:
        if flag == 1:
            counter += 1 #<<<<-------- increasing counter by one every time it loops
            mod_line = list_of_words_to_replace[counter-1] + line.split()[-1] + '\n'  #<---- changed 'mod' to a list of words to replaced.... yes  I know it's counter - 1 because we made counter start at 1 before counting and list index starts at 0 
            fout.write(mod_line)
            if counter > 3: #replaces 3 lines you can replace the number 3 with however many lines you want to override. 
                flag = 0
            continue
        fout.write(line)
        if line.find('section 2') != -1:
            flag = 1
            counter = 0 #<<<<--------- just in case you want to find anotehr section

就像在 cmets 中所说的那样。你在一个写行之后转向flag = 0,所以我们现在有一个计数器来计算你想写多少行,当它超过它时设置flag = 0

还有其他问题吗?

【讨论】:

  • 谢谢和抱歉,当我看到你的代码的结果时,我忘记了一件事:我希望将行的第一个单词替换为不同的单词,比如 mod 1、apple 2、xxx 3。我会更新我的问题。
  • mod_line = 'mod ' + line.split()[-1] + '\n''mod' 替换为您想要的变量。查看编辑后的答案
  • @MooningRawr:作为您在代码中的最后一条评论,我想重复其他部分,因此我将使用“部分”列表而不是模式“第 2 部分”循环,但是有多个单词要每个部分替换,每个部分有不同的单词,有什么想法循环它们吗?
  • 为每个部分创建另一个列表,并将list_of_words_to_replace 替换为正确的部分列表并像这样进行更改。
  • 我已经为这个link 发布了另一个问题。有没有帮助的机会?
【解决方案2】:

您需要重新访问标志更新标准以匹配预期输出。

由于您将flag = 1 设置为if line.find('section 2') != -1 条件,它将只修改匹配行之后的一行。

既然您提到要替换更多行,您可能可以添加一个计数器来跟踪自 flag = 1... 以来您已修改的行数,当您达到所需的行数时,然后重置 @987654324 @。

count = 0

for line in fin:
    if flag == 1 and count < 3:
        mod_line = 'mod ' + line.split()[-1] + '\n'
        count += 1
        fout.write(mod_line)
        continue

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-22
    • 2014-05-24
    • 1970-01-01
    • 1970-01-01
    • 2022-10-02
    相关资源
    最近更新 更多