简体   繁体   English

将前 10 个数字写入 txt 文件,然后使用新值更新 ( python )

[英]Writing the first 10 numbers in a txt file and then updating with the new values ( python )

Im very new to python, and im trying to plot a graph.我对 python 很陌生,我正在尝试 plot 图表。 I have a print () which prints the output of my battery voltage to a txt file called "output.txt.我有一个 print() 将我的电池电压的 output 打印到一个名为“output.txt”的 txt 文件中。

Python Script Python 脚本

 f = open("C:\Scripts\output.txt", "w")

   print("{0:.1f} V".format(voltage/10.0),file=f) #battery voltage

  f.close()

Now the values just keeps updating in the first line of the txt file everytime the script is run.现在,每次运行脚本时,这些值都会在 txt 文件的第一行中不断更新。 And i know its happening because i use the "w".我知道它的发生,因为我使用“w”。

Is there a way that i can write the first 10 values and then start updating the 11 value from the top by deleting the rest of the old values.有没有办法我可以写入前 10 个值,然后通过删除旧值的 rest 从顶部开始更新第 11 个值。 Any help is appreciated.任何帮助表示赞赏。 Thanks a lot for your time!!非常感谢您的时间!!

Since requesting to edit a file means you are asking the os for permission to read/write to the file, then in general for such a small number of values, it would be easiest to simply rewrite all of them at once.由于请求编辑文件意味着您正在请求操作系统读取/写入文件的权限,因此通常对于如此少量的值,一次简单地重写所有值是最简单的。 For example,例如,

def update_file(values):
    with open("C:\Scripts\output.txt", "w") as f:
        for v in values:
            f.write("{0:.1f} V".format(v/10.0))

values = [0]*10
update_file(values)
new_voltage = 11.0
values.pop(0)
values.append(new_voltage)
update_file(values)

Read the file, throw away the first value, add yours and overwrite the file.读取文件,丢弃第一个值,添加你的值并覆盖文件。

Just a thought, it might be easier to just store the data in a list while you are collecting it and then only write the last 10 data points when you are done collecting data.只是一个想法,在收集数据时将数据存储在一个列表中可能会更容易,然后在完成收集数据后只写入最后 10 个数据点。

Here is an example:这是一个例子:

import time

def write_data(file_name, data):
    with open(file_name, 'w') as f:
        for voltage in data:
            f.write("{0:.1f} V".format(voltage/10.0))
            f.write('\n')

if __name__ == "__main__":
    FILE = "C:\Scripts\output.txt"
    N = 10 # maximum number of lines
    data = [0] * N
    for i in range(50): # 50 or however many data points you want to gather
        voltage = 10.15 # Some function that get's the current voltage
        data[i%N] = voltage # 15 % 10 = 5 so we just write over the 6th line when writing the 16th data point.
        time.sleep(1) # however many seconds you want between data points
    
    # Then once you're finished collecting data, go ahead and write it permanently once.
    write_data(FILE, data)

You will still have access to the data in the list data while it is being collected.在收集数据时,您仍然可以访问列表data中的数据。 But here you only need to write to "output.txt" once.但是在这里你只需要写一次“output.txt”。

So data will look like:所以数据看起来像:

[v0, v1, v2, v3, v4, v5, v6, v7, v8, v9] # after the first 10 points have been received
[v10, v11, v12, v3, v4, v5, v6, v7, v8, v9] # after the first 13 points have been received

Then you can write the data once you're done collecting.然后,您可以在完成收集后写入数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM