Python 使用findall替换和添加多行

Python 使用findall替换和添加多行,python,regex,python-3.x,python-2.7,findall,Python,Regex,Python 3.x,Python 2.7,Findall,我试图在一个文件(input.txt)中添加多行,并替换同一文件(input.txt)中的多行,但我的代码只在文件末尾插入这些行。您知道如何修复代码以获得所需的预期输出文件吗 我的代码: import re searchtext1 = """ AB 3.483e-01 2.52e-02 ; 3.46 0.0123 """ # add these lines after searchtext1 addtext1 = """ CD 2.123e-01 1.31e-02 ;

我试图在一个文件(input.txt)中添加多行,并替换同一文件(input.txt)中的多行,但我的代码只在文件末尾插入这些行。您知道如何修复代码以获得所需的预期输出文件吗

我的代码:

import re

searchtext1 = """
AB     3.483e-01   2.52e-02 ; 3.46 0.0123
"""
# add these lines after searchtext1
addtext1 = """
CD     2.123e-01   1.31e-02 ; 7.25 0.0145
DE     4.896e-01   7.25e-02 ; 8.25 0.0185
"""
searchtext2 = """
; atom
#atomnumber

#molecule
[weight]
"""
# replace these lines to searchtext2
changetext2 = """
; iron
#48kcal
35 mol
#12 g
"""

with open('input.txt', 'ab+') as infile:
    matches1 = re.findall(r'^(\d+)\.(.*)$', searchtext1, re.MULTILINE)
    infile.write(addtext1)

    matches2 = re.findall(r'^(\d+)\.(.*)$', searchtext2, re.MULTILINE)
    infile.write(changetext2)
[atom]
123
[bonds]
XY     4.212e-01   4.18e-02 ; 8.01 0.0487
AB     3.483e-01   2.52e-02 ; 3.46 0.0123

[molecule]
1 2
3 4
TY     0.412e-01   1.72e-02 ; 0.32 0.0455

; atom
#atomnumber

#molecule
[weight]
calculated value is 5 kcal/mol
end file
[atom]
123
[bonds]
XY     4.212e-01   4.18e-02 ; 8.01 0.0487
AB     3.483e-01   2.52e-02 ; 3.46 0.0123
CD     2.123e-01   1.31e-02 ; 7.25 0.0145
DE     4.896e-01   7.25e-02 ; 8.25 0.0185

[molecule]
1 2
3 4
TY     0.412e-01   1.72e-02 ; 0.32 0.0455

; iron
#48kcal
35 mol
#12 g
calculated value is 5 kcal/mol
end file
input.txt:

import re

searchtext1 = """
AB     3.483e-01   2.52e-02 ; 3.46 0.0123
"""
# add these lines after searchtext1
addtext1 = """
CD     2.123e-01   1.31e-02 ; 7.25 0.0145
DE     4.896e-01   7.25e-02 ; 8.25 0.0185
"""
searchtext2 = """
; atom
#atomnumber

#molecule
[weight]
"""
# replace these lines to searchtext2
changetext2 = """
; iron
#48kcal
35 mol
#12 g
"""

with open('input.txt', 'ab+') as infile:
    matches1 = re.findall(r'^(\d+)\.(.*)$', searchtext1, re.MULTILINE)
    infile.write(addtext1)

    matches2 = re.findall(r'^(\d+)\.(.*)$', searchtext2, re.MULTILINE)
    infile.write(changetext2)
[atom]
123
[bonds]
XY     4.212e-01   4.18e-02 ; 8.01 0.0487
AB     3.483e-01   2.52e-02 ; 3.46 0.0123

[molecule]
1 2
3 4
TY     0.412e-01   1.72e-02 ; 0.32 0.0455

; atom
#atomnumber

#molecule
[weight]
calculated value is 5 kcal/mol
end file
[atom]
123
[bonds]
XY     4.212e-01   4.18e-02 ; 8.01 0.0487
AB     3.483e-01   2.52e-02 ; 3.46 0.0123
CD     2.123e-01   1.31e-02 ; 7.25 0.0145
DE     4.896e-01   7.25e-02 ; 8.25 0.0185

[molecule]
1 2
3 4
TY     0.412e-01   1.72e-02 ; 0.32 0.0455

; iron
#48kcal
35 mol
#12 g
calculated value is 5 kcal/mol
end file
预期输出文件:

import re

searchtext1 = """
AB     3.483e-01   2.52e-02 ; 3.46 0.0123
"""
# add these lines after searchtext1
addtext1 = """
CD     2.123e-01   1.31e-02 ; 7.25 0.0145
DE     4.896e-01   7.25e-02 ; 8.25 0.0185
"""
searchtext2 = """
; atom
#atomnumber

#molecule
[weight]
"""
# replace these lines to searchtext2
changetext2 = """
; iron
#48kcal
35 mol
#12 g
"""

with open('input.txt', 'ab+') as infile:
    matches1 = re.findall(r'^(\d+)\.(.*)$', searchtext1, re.MULTILINE)
    infile.write(addtext1)

    matches2 = re.findall(r'^(\d+)\.(.*)$', searchtext2, re.MULTILINE)
    infile.write(changetext2)
[atom]
123
[bonds]
XY     4.212e-01   4.18e-02 ; 8.01 0.0487
AB     3.483e-01   2.52e-02 ; 3.46 0.0123

[molecule]
1 2
3 4
TY     0.412e-01   1.72e-02 ; 0.32 0.0455

; atom
#atomnumber

#molecule
[weight]
calculated value is 5 kcal/mol
end file
[atom]
123
[bonds]
XY     4.212e-01   4.18e-02 ; 8.01 0.0487
AB     3.483e-01   2.52e-02 ; 3.46 0.0123
CD     2.123e-01   1.31e-02 ; 7.25 0.0145
DE     4.896e-01   7.25e-02 ; 8.25 0.0185

[molecule]
1 2
3 4
TY     0.412e-01   1.72e-02 ; 0.32 0.0455

; iron
#48kcal
35 mol
#12 g
calculated value is 5 kcal/mol
end file

您已在附加模式“ab+”下打开文件,该模式指定所有写入操作都将转到文件末尾。若要按所需方式更改文件的某些部分,必须重写整个文件


一种常见的模式是创建一个新的临时文件,写入新数据,然后将新文件移到旧文件上。这样,覆盖是原子性的,如果程序崩溃,丢失数据的可能性更小

正如Chris在评论中提到的,我建议尝试使用一个已经可以与格式交互的库-
configparser
对我来说似乎是一个明显的选择,但是如果格式与您的问题不同,可能有理由不这样做

除此之外,如果您想使用
re
,我已经稍微更新了您的代码。以下是我所做的调整:

  • 从三重引号字符串的开头和结尾删除换行符,因为它们将成为匹配的一部分,这可能是您想要的,也可能不是您想要的。我个人的偏好是在实际替换中明确新行

  • 在搜索文本中转义的
    [
    ]
    ,因为它们是
    re
    中的特殊字符,会混淆它(它们指定了字符类)

  • 与一起使用以打开单独的输入和输出文件。当您使用块退出
    时,这些将被清除。如果你真的想替换input.txt,我想你可以把output.txt移到上面

  • 您可以使用
    re.sub
    直接替换引用,而不是搜索它们,在文本中查找索引,然后替换/追加。在添加文本的情况下,我刚刚对
    searchtext1
    addtext1
    进行了替换。(我使用了一个f字符串,但是如果您愿意,也可以执行
    “{search}\n{add}.”格式(search=searchtext,add=addtext1)

  • 最后,我们将更新后的文本写回光盘

希望有帮助

import re

searchtext1 = """AB     3.483e-01   2.52e-02 ; 3.46 0.0123"""
# add these lines after searchtext1
addtext1 = """CD     2.123e-01   1.31e-02 ; 7.25 0.0145
DE     4.896e-01   7.25e-02 ; 8.25 0.0185"""

searchtext2 = """; atom
#atomnumber

#molecule
\[weight\]"""
# replace these lines to searchtext2
changetext2 = """; iron
#48kcal
35 mol
#12 g"""

with open('input.txt', 'r') as infile, open("output.txt", "w") as outfile:
    intext = infile.read()

    intext = re.sub(searchtext1, f"{searchtext1}\n{addtext1}", intext)
    intext = re.sub(searchtext2, changetext2, intext)

    outfile.write(intext)

我该怎么做?在你的input.txt中,那是什么语言?我会检查是否有一个python库可以解析它,然后你可以在概念上与它交互,而不是直接读取和写入语法。这是一个普通的文本文件。