在Python中,如何在X个时间之后替换文件中的字符串?

在Python中,如何在X个时间之后替换文件中的字符串?,python,Python,前提解释: # Main.py while True: f=open("data.txt","a+") a = str(scan())+"\n" f.write(a); log.log("Wrote to file") log.log(a) with open("data.txt","rt") as fin: w

前提解释:

# Main.py

while True:
    f=open("data.txt","a+")
    a = str(scan())+"\n"
    f.write(a);
    log.log("Wrote to file")
    log.log(a)
    with open("data.txt","rt") as fin:
        with open("data.txt","wt")as fout:
            for line in fin:
                fout.write(line.replace("scan()", "scan()"))
    f.close()
[26.07, 32.31, 93.73, False] #it constantly overwrites this line.
    #print out 10 strings of text
    [23.07, 32.31, 93.73, False] 
    [27.03, 36.34, 93.73, False] 
    [26.07, 34.36, 93.73, False] 
    [28.02, 32.21, 93.73, False] 
    [24.03, 22.31, 93.73, False] 
    [22.07, 28.31, 93.73, False] 
    [29.04, 32.21, 93.73, False] 
    [26.07, 32.31, 93.73, False] 
    [26.07, 32.31, 93.73, False] 
    [26.07, 32.31, 93.73, False]

#new string of text comes in [26.07, 32.31, 93.73, True]
expected output:
   [27.03, 36.34, 93.73, False] 
   [26.07, 34.36, 93.73, False] 
   [28.02, 32.21, 93.73, False] 
   [24.03, 22.31, 93.73, False] 
   [22.07, 28.31, 93.73, False] 
   [29.04, 32.21, 93.73, False] 
   [26.07, 32.31, 93.73, False] 
   [26.07, 32.31, 93.73, False] 
   [26.07, 32.31, 93.73, False] 
   [26.07, 32.31, 93.73, True]  # replaces the string one, adding this new data into the list.
我有一个Python脚本Main.py,它收集距离和温度,并将它们发送到一个文本文件Data.txt。目前我有它,所以每次它都会替换文本字符串

编辑:

# Main.py

while True:
    f=open("data.txt","a+")
    a = str(scan())+"\n"
    f.write(a);
    log.log("Wrote to file")
    log.log(a)
    with open("data.txt","rt") as fin:
        with open("data.txt","wt")as fout:
            for line in fin:
                fout.write(line.replace("scan()", "scan()"))
    f.close()
[26.07, 32.31, 93.73, False] #it constantly overwrites this line.
    #print out 10 strings of text
    [23.07, 32.31, 93.73, False] 
    [27.03, 36.34, 93.73, False] 
    [26.07, 34.36, 93.73, False] 
    [28.02, 32.21, 93.73, False] 
    [24.03, 22.31, 93.73, False] 
    [22.07, 28.31, 93.73, False] 
    [29.04, 32.21, 93.73, False] 
    [26.07, 32.31, 93.73, False] 
    [26.07, 32.31, 93.73, False] 
    [26.07, 32.31, 93.73, False]

#new string of text comes in [26.07, 32.31, 93.73, True]
expected output:
   [27.03, 36.34, 93.73, False] 
   [26.07, 34.36, 93.73, False] 
   [28.02, 32.21, 93.73, False] 
   [24.03, 22.31, 93.73, False] 
   [22.07, 28.31, 93.73, False] 
   [29.04, 32.21, 93.73, False] 
   [26.07, 32.31, 93.73, False] 
   [26.07, 32.31, 93.73, False] 
   [26.07, 32.31, 93.73, False] 
   [26.07, 32.31, 93.73, True]  # replaces the string one, adding this new data into the list.
目前它只打印一个文本字符串,代码会重复地覆盖这一行,我想要的是代码在覆盖最早的一行之前打印该字符串的10行,就像下面的示例一样

示例:

# Main.py

while True:
    f=open("data.txt","a+")
    a = str(scan())+"\n"
    f.write(a);
    log.log("Wrote to file")
    log.log(a)
    with open("data.txt","rt") as fin:
        with open("data.txt","wt")as fout:
            for line in fin:
                fout.write(line.replace("scan()", "scan()"))
    f.close()
[26.07, 32.31, 93.73, False] #it constantly overwrites this line.
    #print out 10 strings of text
    [23.07, 32.31, 93.73, False] 
    [27.03, 36.34, 93.73, False] 
    [26.07, 34.36, 93.73, False] 
    [28.02, 32.21, 93.73, False] 
    [24.03, 22.31, 93.73, False] 
    [22.07, 28.31, 93.73, False] 
    [29.04, 32.21, 93.73, False] 
    [26.07, 32.31, 93.73, False] 
    [26.07, 32.31, 93.73, False] 
    [26.07, 32.31, 93.73, False]

#new string of text comes in [26.07, 32.31, 93.73, True]
expected output:
   [27.03, 36.34, 93.73, False] 
   [26.07, 34.36, 93.73, False] 
   [28.02, 32.21, 93.73, False] 
   [24.03, 22.31, 93.73, False] 
   [22.07, 28.31, 93.73, False] 
   [29.04, 32.21, 93.73, False] 
   [26.07, 32.31, 93.73, False] 
   [26.07, 32.31, 93.73, False] 
   [26.07, 32.31, 93.73, False] 
   [26.07, 32.31, 93.73, True]  # replaces the string one, adding this new data into the list.
实际输出:

# Main.py

while True:
    f=open("data.txt","a+")
    a = str(scan())+"\n"
    f.write(a);
    log.log("Wrote to file")
    log.log(a)
    with open("data.txt","rt") as fin:
        with open("data.txt","wt")as fout:
            for line in fin:
                fout.write(line.replace("scan()", "scan()"))
    f.close()
[26.07, 32.31, 93.73, False] #it constantly overwrites this line.
    #print out 10 strings of text
    [23.07, 32.31, 93.73, False] 
    [27.03, 36.34, 93.73, False] 
    [26.07, 34.36, 93.73, False] 
    [28.02, 32.21, 93.73, False] 
    [24.03, 22.31, 93.73, False] 
    [22.07, 28.31, 93.73, False] 
    [29.04, 32.21, 93.73, False] 
    [26.07, 32.31, 93.73, False] 
    [26.07, 32.31, 93.73, False] 
    [26.07, 32.31, 93.73, False]

#new string of text comes in [26.07, 32.31, 93.73, True]
expected output:
   [27.03, 36.34, 93.73, False] 
   [26.07, 34.36, 93.73, False] 
   [28.02, 32.21, 93.73, False] 
   [24.03, 22.31, 93.73, False] 
   [22.07, 28.31, 93.73, False] 
   [29.04, 32.21, 93.73, False] 
   [26.07, 32.31, 93.73, False] 
   [26.07, 32.31, 93.73, False] 
   [26.07, 32.31, 93.73, False] 
   [26.07, 32.31, 93.73, True]  # replaces the string one, adding this new data into the list.
问题:

# Main.py

while True:
    f=open("data.txt","a+")
    a = str(scan())+"\n"
    f.write(a);
    log.log("Wrote to file")
    log.log(a)
    with open("data.txt","rt") as fin:
        with open("data.txt","wt")as fout:
            for line in fin:
                fout.write(line.replace("scan()", "scan()"))
    f.close()
[26.07, 32.31, 93.73, False] #it constantly overwrites this line.
    #print out 10 strings of text
    [23.07, 32.31, 93.73, False] 
    [27.03, 36.34, 93.73, False] 
    [26.07, 34.36, 93.73, False] 
    [28.02, 32.21, 93.73, False] 
    [24.03, 22.31, 93.73, False] 
    [22.07, 28.31, 93.73, False] 
    [29.04, 32.21, 93.73, False] 
    [26.07, 32.31, 93.73, False] 
    [26.07, 32.31, 93.73, False] 
    [26.07, 32.31, 93.73, False]

#new string of text comes in [26.07, 32.31, 93.73, True]
expected output:
   [27.03, 36.34, 93.73, False] 
   [26.07, 34.36, 93.73, False] 
   [28.02, 32.21, 93.73, False] 
   [24.03, 22.31, 93.73, False] 
   [22.07, 28.31, 93.73, False] 
   [29.04, 32.21, 93.73, False] 
   [26.07, 32.31, 93.73, False] 
   [26.07, 32.31, 93.73, False] 
   [26.07, 32.31, 93.73, False] 
   [26.07, 32.31, 93.73, True]  # replaces the string one, adding this new data into the list.
如何使其在替换字符串之前打印出至少10行文本

需要的输出示例:

# Main.py

while True:
    f=open("data.txt","a+")
    a = str(scan())+"\n"
    f.write(a);
    log.log("Wrote to file")
    log.log(a)
    with open("data.txt","rt") as fin:
        with open("data.txt","wt")as fout:
            for line in fin:
                fout.write(line.replace("scan()", "scan()"))
    f.close()
[26.07, 32.31, 93.73, False] #it constantly overwrites this line.
    #print out 10 strings of text
    [23.07, 32.31, 93.73, False] 
    [27.03, 36.34, 93.73, False] 
    [26.07, 34.36, 93.73, False] 
    [28.02, 32.21, 93.73, False] 
    [24.03, 22.31, 93.73, False] 
    [22.07, 28.31, 93.73, False] 
    [29.04, 32.21, 93.73, False] 
    [26.07, 32.31, 93.73, False] 
    [26.07, 32.31, 93.73, False] 
    [26.07, 32.31, 93.73, False]

#new string of text comes in [26.07, 32.31, 93.73, True]
expected output:
   [27.03, 36.34, 93.73, False] 
   [26.07, 34.36, 93.73, False] 
   [28.02, 32.21, 93.73, False] 
   [24.03, 22.31, 93.73, False] 
   [22.07, 28.31, 93.73, False] 
   [29.04, 32.21, 93.73, False] 
   [26.07, 32.31, 93.73, False] 
   [26.07, 32.31, 93.73, False] 
   [26.07, 32.31, 93.73, False] 
   [26.07, 32.31, 93.73, True]  # replaces the string one, adding this new data into the list.

你可以考虑使用<代码> Time.Scess()/Script >,以便每秒只写一次文件。

import time
import numpy as np

def scan():
    # your scanner here. 
    return list(np.random.randint(1, 10, 4))

a_list = list()

while True:
    a = str(scan()) + "\n"
    a_list.insert(0, a)
    a_list = a_list[:10]

    with open("data.txt", "wt") as fout:
        fout.write(''.join(a_list))
        time.sleep(1)
试试这个:

while True:
    txt = open("data.txt", "r").read()
    txt = txt.split("\n")
    if len(txt) == 10:
        txt.pop(0)
        txt.append("what you want to write")
    else:
        txt.append("what you want to write")
    f = open("data.txt", "w")
    f.write("\n".join(txt))

谢谢你的建议,它确实像预期的那样附加了,但由于某些原因,即使在删除时间之后,它仍然非常慢。sleep@yongjie可能是因为你的扫描功能,扫描功能仍然每1秒打印一次,所以我认为这不是问题所在,我想这可能是因为我使用的是覆盆子Pi,它可能没有足够的处理能力?谢谢你的建议,我已经尝试了你的解决方案,但不幸的是,我无法让它打印scan()字符串。我尝试了
txt.append(str=(scan())
但是没有用,你有解决办法吗?你可以将扫描返回值存储在一个变量中,然后在txtAh中附加一个字符串谢谢,我现在让它运行了,但我相信这不是我要找的函数,我要找的文本长度代码检查就是保持一个常量行,就像我的示例中一样。Regardless,再次感谢你的时间,它仍然帮助我进一步理解python。欢迎你,如果你编辑你的答案澄清问题,我将很乐意帮助你