所以我用的是樹莓派和感覺帽。我正在嘗試撰寫代碼以每分鐘查找溫度、濕度和壓力,然后將其保存到檔案中。我已經撰寫了每分鐘查找溫度、壓力和濕度的代碼。
from sense_hat import SenseHat
from time import time, sleep
while True:
sleep(1 - time() % 1) #
sense = SenseHat()
# Take readings from all three sensors
t = pressure = sense.get_pressure()
print(pressure)
p = temp = sense.get_temperature()
print(temp)
h = humidity = sense.get_humidity()
print(humidity)
# Round the values to one decimal place
t = round(t, 1)
p = round(p, 1)
h = round(h, 1)
# Create the message
# str() converts the value to a string so it can be concatenated
message = "Temperature: " str(t) " Pressure: " str(p) " Humidity: " str(h)
# Display the scrolling message
sense.show_message(message, scroll_speed=0.05)
from time import time, sleep
while True:
sleep(60 - time() % 60)
現在我需要將輸出資訊保存到檔案中。我知道我可以使用以下方法附加我想說的任何內容:
f = open("sensedata.txt", "a")
f.write("temp, pressure, humidity")
f.close()
#open and read the file after the appending:
f = open("sensedata.text", "r")
print(f.read())
但是我需要能夠附加我已經找到的資料,即溫度壓力和濕度。我嘗試附加,
" t \n, h \n, p \n,"因為我已經定義了它t = temp p = pressure,h = humidity但它確實出現在檔案中作為 t \n, h \n, p \n. 我不知道我該怎么做。
uj5u.com熱心網友回復:
用它:
f = open("sensedata.txt", "a")
f.write(f" {t} \n, {h} \n, {p} \n,")
f.close()
要獲取有關在 python 中使用字串的更多資訊,請單擊此處和此處
python中的字串連接:
1-簡單:
a="welcome"
b="stackoverflow"
print(a " to " b)
輸出是:
welcome to stackoverflow
注意:在這個方法中a并且b必須str
2-f 字串
print(f'{a} to {b}')
三格式
print(('{} to {}').format(a, b))
uj5u.com熱心網友回復:
將讀取和寫入分離到不同的功能中是一個好主意。
附加到您想用 mode 打開它的檔案,a并在第一次用 mode 打開它時創建它w。
將結果作為 CSV 檔案以供以后處理通常有助于傳感器讀數。
當滾動顯示屏上的文本可能需要不同的時間長度時,每分鐘讀取一次傳感器是很困難的。如果分鐘值在時鐘上發生變化,可能更容易查看。
下面是一個示例:
import csv
from datetime import datetime
from pathlib import Path
from time import sleep
from sense_hat import SenseHat
CSV_LOG = Path('/tmp/sensor_readings.csv')
sense = SenseHat()
def read_sensor():
return {
"Time": datetime.utcnow().isoformat(),
"Pressure": round(sense.get_pressure(), 1),
"Temperature": round(sense.get_temperature(), 1),
"Humidity": round(sense.get_humidity(), 1),
}
def show_reading(sensor_reading):
message = f'{sensor_reading=}'
print(message)
sense.show_message(message, scroll_speed=0.05)
def write_reading(sensor_reading):
first_write = not CSV_LOG.exists()
open_mode = 'w' if first_write else 'a'
with CSV_LOG.open(open_mode) as csvfile:
fieldnames = ["Time", "Temperature", "Pressure", "Humidity"]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
if first_write:
writer.writeheader()
writer.writerow(sensor_reading)
def main():
last_reading_minute = -1
while True:
if datetime.now().minute != last_reading_minute:
last_reading_minute = datetime.now().minute
sensor_values = read_sensor()
show_reading(sensor_values)
write_reading(sensor_values)
sleep(10)
if __name__ == '__main__':
main()
我的測驗列印出以下內容:
sensor_reading={'Time': '2021-11-07T18:07:29.445745', 'Pressure': 63, 'Temperature': 33, 'Humidity': 45}
sensor_reading={'Time': '2021-11-07T18:08:29.455906', 'Pressure': 72, 'Temperature': 25, 'Humidity': 37}
sensor_reading={'Time': '2021-11-07T18:09:29.458768', 'Pressure': 45, 'Temperature': 24, 'Humidity': 71}
并將以下內容寫入/tmp/sensor_readings.csv:
Time,Temperature,Pressure,Humidity
2021-11-07T18:07:29.445745,33,63,45
2021-11-07T18:08:29.455906,25,72,37
2021-11-07T18:09:29.458768,24,45,71
注意:與while程式中的無限回圈相比,創建crontab或systemd 計時器以在正確的時間間隔運行 python 腳本可能更有效。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/352722.html
上一篇:用Python取矩陣中的元素
