通过计算时间来代替t = 0.000000

时间:2018-12-19 21:01:18

标签: python-3.x replace

我使用此代码将t = 0.000000替换为一个计数器(时间= time + 10),因此每次代码找到t = 0.000000时,它将加10并变为0、10、20,... 任何想法如何做到这一点

import re
import itertools
counter = itertools.count(0, 10)
with open("input.txt", "r") as f_in:
with open("output.txt", "w") as f_out:
    for line in f_in:
        re.sub("t=0.000000", f"t={next(counter):.6f}", line.lower())
        f_out.write(line + "/n")

1 个答案:

答案 0 :(得分:0)

您可能想要这样的东西:

import re
import itertools
counter = itertools.count(0, 10)
with open("input.txt", "r") as f_in:
    with open("output.txt", "w") as f_out:
        for line in f_in:
            re.sub("t=0.000000", f"t += {next(counter)}", line.lower())
            f_out.write(line + "/n")

count给出10的倍数的无穷序列,用sub替换旧的部分为新的部分。

编辑:

只是注意到您想要不同的输出,因此相应地更改了字符串:

import re
import itertools
counter = itertools.count(0, 10)
with open("input.txt", "r") as f_in:
    with open("output.txt", "w") as f_out:
        for line in f_in:
            re.sub("t=0.000000", f"t={next(counter):.6f}", line.lower())
            f_out.write(line + "/n")