【问题标题】:Replace strings in file with list values用列表值替换文件中的字符串
【发布时间】:2026-01-08 12:50:01
【问题描述】:

我有一个列表: 结果=[[0.0, 12.053600000000001], [0.01, 14.2272], [0.02, 15.314000000000002], [0.04, 18.5744], [0.05, -18.772000000000002]], [0.67, -1.54]

我有一个 in.txt 文件,其中包含以下值:

NPTH 6
THTIM 
  0.0  0.00  0.001  -1.22
  0.01  0.123 0.550  -1.44
  0.02  0.22  0.440  -1.55
  0.04  0.456  0.220  -1.88
  0.05  0.788  0.005  1.9
  0.67  0.23  0.340   0.2

NPPD 4
1.0 5.0 8.0
1.0 4.0 2.0
2.0 5.0 2.0
4.0 5.0 2.0    

THTIM 5
2.0 1.0
1.0 2.0
1.0 1.0

我需要用结果列表中每个列表中的第二个参数替换 THTIM 之后的第二列,即 in.txt 文件中的字符串 0.00,0.123,0.22,0.456,0.788,0.23 需要替换为 12.053600000000001,14.2272,15.314000000000002,分别为 18.5744、-18.772000000000002、-1.54。 即我需要我的输出为

NPTH 6
THTIM 
0.00  12.053600000000001  0.001  -1.22
0.01  14.2272             0.550  -1.44
0.02  15.314000000000002  0.440  -1.55
0.04  18.5744             0.220  -1.88
0.05  -18.772000000000002 0.005  1.9
0.67  -1.54              0.340   0.2    

NPPD 4
1.0 5.0 8.0
1.0 4.0 2.0
2.0 5.0 2.0
4.0 5.0 2.0

THTIM 5
2.0 1.0
1.0 2.0
1.0 1.0

我试过的是:

f3=open("in.txt" ,'r')
import re
result=[[0.0, 12.053600000000001], [0.01, 14.2272], [0.02,15.314000000000002], [0.04, 18.5744], [0.05, -18.772000000000002], [0.67, -1.54]]
o2=open("out.txt" ,'w')
thtm2Cnt=0
thtm2Flag=0
for ot in f3.readlines():
    ou=ot
    print(ot)
    if re.match('NPTH',ou):

        o2.write(ou)
        strplt =ou.split()
        cnt=int(strplt[1])
    elif re.match('THTIM',ou):
        thtm2Flag=1

        o2.write(ou)
    elif thtm2Flag==1:
        if thtm2Cnt<=cnt-1:
            strplt=ou.split()
            ou = strplt[0] + "\t" +  "\t" + strplt[2] + "\t" + strplt[3] + " \n        "
            o2.write(ou)
            thtm2Cnt+=1
        elif thtm2Cnt==cnt:
            thtm2Cnt=0
            thtm2Flag=0
            o2.write(ou)
    else:
        o2.write(ou)

请提供代码以实现此目的。 (编辑:导入 re 并声明变量)

【问题讨论】:

    标签: python string file arraylist replace


    【解决方案1】:

    您可以在 Jupyter notebook 或 Python IDE 中遵循这些:

    【讨论】:

    • 。它只在输出文件中写入替换的字符串。我希望输入的全部内容作为输出并仅替换第二列。我希望我的输出为NPTH 6 THTIM 0.20 12.05360000000000001 0.001 -1.22 0.22 14.2272 0.1550 -1.44 0.15.31400000000000002 0.440 -1.55 0.43 18.5744 0.220 -1.88 0.34 -18.7720000000000000002 0.220 -1.88 0.0.0 0.005 1.9 0.33 -1.54 0.340 0.2 NPPD 4 1.0 5.0 8.0 1.0 4.0 2.0 2.0 5.0 2.0 4.0 5.0 2.0。请帮忙
    • 请提供代码以实现所需的输出。
    • 请提供代码以实现所需的输出。
    • 我已经更新了我的要求。请帮助解决这个问题。
    【解决方案2】:

    你想用这个吗?

    import re
    replace_column=["12.053600000000001", "14.2272","15.314000000000002", "18.5744", "-18.772000000000002", "-1.54"]
    
    def process(part) :
        if re.search("NPTH \d",part) and re.search("THTIM",part):
            lines = part.split("\n")
            grid = [line.split() for line in lines[2:]]
            result = []
            for idx, row in enumerate(grid):
                newrow = []
                for index, cell in enumerate(row):
                    if index == 1:
                        newrow = newrow + [replace_column[idx]]
                    else:
                        newrow = newrow + [cell]
                result = result + [newrow]
            return "\n".join(lines[:2] + ["\t".join(row) for row in result])
        else:
            return part
    
    fr=open('in.txt','r')
    fw=open('out.txt','w')
    f = fr.read()
    fr.close()
    parts = f.split("\n\n")
    fw.write("\n\n".join([process(part) for part in parts]))
    

    【讨论】:

    • 我已经更新了我的要求。我只需要更改第一个 thtim 块的值请帮助解决此问题
    • 。它没有更新值。请提供代码以实现上述所需的输出。
    • 无法写入映射,因为它应该自动从文件中获取值。不能分配给它。有没有其他方法可以实现这一点。
    • .有没有其他方法没有映射为mapping={0.0: 12.053600000000001, 0.01: 14.2272,0.02: 15.314000000000002,0.04: 18.5744,0.05: -18.772000000000这应该会自动从文件和地图中读取。请帮助实现这一点。
    • 您可以使用开关。你能更好地解释映射吗?您使用文件进行映射?