如何动态填充 json 文件
How to fill dynamically a json file
我正在使用此代码块将一些数据从 Pi 发送到云端(需要采用 json 格式)。不过,我还想 将数据保存在创建的 json 文件中 。关于发送到云的一切都有效,因为我可以一个一个地发送值,但在 json 文件中我遇到了一些问题。任何人都可以通过我的命令看到:
with open('json_body.json', 'w') as json_file:
json.dump(data, json_file)
我创建了一个 json 文件,但只保存了 最新的“数据”值 。有谁知道我怎样才能保存所有这些?我是否必须先使用数组或列表来保存它们?
任何建议都是有帮助的。提前致谢!
while True:
acc = ReadChannel(acc_channel)
# Print out results
print("--------------------------------------------")
print("Acc : " , acc , ",Acc in volts", ConvertVolts(acc,2))
data = {}
data['acceleration'] = acc
json_body = json.dumps(data)
print("Sending message: ", json_body)
counter = counter + 1
print(counter)
with open('json_body.json', 'w') as json_file:
json.dump(data, json_file)
await device_client.send_message(json_body)
# Wait before repeating loop
time.sleep(delay)
await device_client.disconnect()
不需要每次迭代都写文件操作可以尝试在循环结束后一次写入所有数据。
temporary_json = []
while True:
acc = ReadChannel(acc_channel)
# Print out results
print("--------------------------------------------")
print("Acc : ", acc, ",Acc in volts", ConvertVolts(acc, 2))
data = {}
data["acceleration"] = acc
json_body = json.dumps(data)
print("Sending message: ", json_body)
counter = counter + 1
print(counter)
<b>temporary_json.append(json.dumps(data))</b>
await device_client.send_message(json_body)
# Wait before repeating loop
time.sleep(delay)
with open("json_body.json", "w") as json_file:
<b>proper_json = ",".join(temporary_json) # making a valid json out of temporary json</b>
json.dump(proper_json, json_file)
awaitdevice_client.disconnect()</pre>
我正在使用此代码块将一些数据从 Pi 发送到云端(需要采用 json 格式)。不过,我还想 将数据保存在创建的 json 文件中 。关于发送到云的一切都有效,因为我可以一个一个地发送值,但在 json 文件中我遇到了一些问题。任何人都可以通过我的命令看到:
with open('json_body.json', 'w') as json_file:
json.dump(data, json_file)
我创建了一个 json 文件,但只保存了 最新的“数据”值 。有谁知道我怎样才能保存所有这些?我是否必须先使用数组或列表来保存它们? 任何建议都是有帮助的。提前致谢!
while True:
acc = ReadChannel(acc_channel)
# Print out results
print("--------------------------------------------")
print("Acc : " , acc , ",Acc in volts", ConvertVolts(acc,2))
data = {}
data['acceleration'] = acc
json_body = json.dumps(data)
print("Sending message: ", json_body)
counter = counter + 1
print(counter)
with open('json_body.json', 'w') as json_file:
json.dump(data, json_file)
await device_client.send_message(json_body)
# Wait before repeating loop
time.sleep(delay)
await device_client.disconnect()
不需要每次迭代都写文件操作可以尝试在循环结束后一次写入所有数据。
temporary_json = []
while True:
acc = ReadChannel(acc_channel)
# Print out results
print("--------------------------------------------")
print("Acc : ", acc, ",Acc in volts", ConvertVolts(acc, 2))
data = {}
data["acceleration"] = acc
json_body = json.dumps(data)
print("Sending message: ", json_body)
counter = counter + 1
print(counter)
<b>temporary_json.append(json.dumps(data))</b>
await device_client.send_message(json_body)
# Wait before repeating loop
time.sleep(delay)
with open("json_body.json", "w") as json_file:
<b>proper_json = ",".join(temporary_json) # making a valid json out of temporary json</b>
json.dump(proper_json, json_file)
awaitdevice_client.disconnect()</pre>