脚本将运行,但仅输出文本文件中的最后一项。我想从第一项开始。变量为timestamp
和val
。每次读取文件后,它们的值都会更改。
到目前为止,我已经拥有了
while True:
with open ("time.txt",'r') as FF:
for line in FF:
lines.append(line)
timestamp = line
with open ("val.txt",'r') as PP:
for line in PP:
lines.append(line)
val = line
data= '{ "timestamp": "' + timestamp + '","temp": ' + str(val) + '}'
print(data) #print payload for reference
我想我在每个循环之后都缺少增加的内容...但是我不知道如何在这里实现它。
期望的是:
2020-02-22T23:50:00Z
24
感谢您的光临!
答案 0 :(得分:1)
如果您只想从两个文件中打印匹配的timestamp
和val
对,则可以使用zip
来同时迭代两个文件:
with open ("time.txt",'r') as FF, open ("val.txt",'r') as PP:
for timestamp, val in zip(FF, PP):
data= '{ "timestamp": "' + timestamp + '","temp": ' + val + '}'
print(data) #print payload for reference