Python 回复:为什么我的替换文件中的值的代码没有抛出错误或更改值?

问题描述 投票:0回答:1

我需要用 Pandas 数据框中的新值替换外部文件(“

file.txt
”)中的某些值。外部文件内容如下所示:

(Many lines of comments, then)
identifier1       label2 = i \ label3        label4                                  
label5
A1 = -5563.88 B2 = -4998 C3 = -203.8888 D4 = 5926.8 
E5 = 24.99876 F6 = 100.6666 G7 = 30.008 H8 = 10.9999
J9 = 1000000 K10 = 1.0002 L11 = 0.1
M12

identifier2       label2 = i \ label3        label4                                  
label5
A1 = -788 B2 = -6554 C3 = -100.23 D4 = 7526.8 
E5 = 20.99876 F6 = 10.6666 G7 = 20.098 H8 = 10.9999
J9 = 1000000 K10 = 1.0002 L11 = 0.000
M12
...

从这里之前的帖子,this资源,以及Python的“re”,我正在尝试:

findThisIdentifierInFile = "identifier1" # I want the data immediately below this identifier in the external file

with open("file.txt", "r") as file:
    file_string = file.read()

    i = -500 # New A1 value (i.e., I want to replace the A1 value in the file with -500).
    j = 100  # New C3 value.  

    string1 = re.sub(
        rf"^({findThisIdentifierInFile}\s.*?)A1 = \S+ C3 = \S+",
        f"\g<1>A1 = {i} C3 = {j}",
        string1,
        flags=re.M | re.S,
    )  

当我运行它时,没有错误,但什么也没有发生。例如,当我打印“

string1
”时,数据与原始“
file.txt
”中的数据相同。我无法提供更多的代码,但希望有 RegEx 和 re (Python) 经验的人能够发现我出错的地方。我提前道歉,因为我肯定做了一些愚蠢的事情。

有时我还想替换

B2
值和
E5
-
H8
值以及其他行上的值。我想知道是否有一种更简单/新手友好的方法可以用来对紧邻特定识别标签下方的值进行任何可能的替换。

python python-3.x pandas replace python-re
1个回答
0
投票

IIUC,您可以分多个步骤进行字符串替换,例如:

import re

text = r"""
(Many lines of comments, then)
identifier1       label2 = i \ label3        label4
label5
A1 = -5563.88 B2 = -4998 C3 = -203.8888 D4 = 5926.8
E5 = 24.99876 F6 = 100.6666 G7 = 30.008 H8 = 10.9999
J9 = 1000000 K10 = 1.0002 L11 = 0.1
M12

identifier2       label2 = i \ label3        label4
label5
A1 = -788 B2 = -6554 C3 = -100.23 D4 = 7526.8
E5 = 20.99876 F6 = 10.6666 G7 = 20.098 H8 = 10.9999
J9 = 1000000 K10 = 1.0002 L11 = 0.000
M12
..."""


def my_replace_function(g):
    i = -500  # New A1 value
    j = 100  # New C3 value

    s = re.sub(r"A1 = \S+", f"A1 = {i}", g.group(2))
    s = re.sub(r"C3 = \S+", f"C3 = {j}", s)

    return g.group(1) + s + "\n\n"


findThisIdentifierInFile = "identifier1"
text = re.sub(
    rf"^({findThisIdentifierInFile})(.*?)\n\n",
    my_replace_function,
    text,
    flags=re.M | re.S,
)
print(text)

打印:


(Many lines of comments, then)
identifier1       label2 = i \ label3        label4
label5
A1 = -500 B2 = -4998 C3 = 100 D4 = 5926.8
E5 = 24.99876 F6 = 100.6666 G7 = 30.008 H8 = 10.9999
J9 = 1000000 K10 = 1.0002 L11 = 0.1
M12

identifier2       label2 = i \ label3        label4
label5
A1 = -788 B2 = -6554 C3 = -100.23 D4 = 7526.8
E5 = 20.99876 F6 = 10.6666 G7 = 20.098 H8 = 10.9999
J9 = 1000000 K10 = 1.0002 L11 = 0.000
M12
...
© www.soinside.com 2019 - 2024. All rights reserved.