简体   繁体   English

如何在 JSON 文件中重复保存 python output 值

[英]How can i repeatedly save the python output values in a JSON file

Hey guys this is probably such an amateur question to ask but how do i constantly list the latency value.嘿,伙计们,这可能是一个业余问题,但我如何不断列出延迟值。 As you can see in the Terminal i have several values but they get overwritten for each new value.正如您在终端中看到的那样,我有几个值,但是每个新值都会覆盖它们。 how do i print all of the new python output values in a list like for example我如何在列表中打印所有新的 python output 值,例如

  1. {"Latency": "0.039332s for the calculation"} {“延迟”:“计算时间为 0.039332 秒”}
  2. {"Latency": "0.025932s for the calculation"} {“延迟”:“计算时间为 0.025932 秒”}
  3. {"Latency": "0.032072s for the calculation"} {“延迟”:“计算时间为 0.032072 秒”}
  4. {"Latency": "0.049562s for the calculation"} and so on... instead of just one {"Latency": "0.049562s for the calculation"} 等等...而不是只有一个
  5. {"Latency": "0.039332s for the calculation"} which gets overwritten with each new value {"Latency": "0.039332s for the calculation"} 被每个新值覆盖
from time import sleep
import datetime
import pymongo
import time
import json
# This URL provides connection to the database
uri = blahblah

# initialising pymongo client
client = pymongo.MongoClient(uri)

# Database where the records will be saved - reference to the database
db = client.Kostenanalyse

# Accessing the collection "latenz" from the Database
coll = db.latenz

#Defining the Start time
start_time = datetime.datetime.now()
start_time = datetime.datetime.now().isoformat()
end = time.perf_counter()

# Opens a file to read current temperature
with open(r"/sys/class/thermal/thermal_zone0/temp") as File:
        ActualTemp = int(File.readline())/float(1000)


def create_info_data()-> dict:
 
 return {
       "CurrentTemp in °C" : ActualTemp,
       "Time when packet was sent" : datetime.datetime.now().isoformat(),
       "Sensor reading" : "",
       "Latency" : end,

}

def writeToJSONFile(path, fileName, data):
    filePathNameWExt = './' + path + '/' + fileName + '.json'
    with open(filePathNameWExt, 'w') as fp:
        json.dump(data, fp)




#While loop 
while True:
    data = create_info_data()

    start = time.perf_counter()

    coll.insert_one(data)

    end = time.perf_counter() - start

    print('{:.6f}s for the calculation'.format(end))
    data = {}
    data['Latency'] = '{:.6f}s for the calculation'.format(end) 
    writeToJSONFile('./','latency',data)

    print(str(start_time) + str(float(ActualTemp)) + 'Wrote data sample {} to collpipection {}'.format(data, 'info'))

    sleep(0.5) 

My python code + Terminal + Json file as a screenshot on linux我的 python 代码 + 终端 + Json 文件作为 linux 上的屏幕截图

If you open a file with the w mode, you will always start writing to it at the beginning of the file.如果您使用w模式打开文件,您将始终在文件开头开始写入。 To append to a file, open the file with the a mode, this will let you start writing to the end of the file instead:将 append 写入文件,以a模式打开文件,这将让您开始写入文件末尾:

with open(filePathNameWExt, 'a') as fp:
    # write to fp will not overwrite existing data

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

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