我正在设置一个树莓派零来感知空气质量、温度和湿度。我能够运行脚本以每 60 秒获取一次检测到的数据。如果我需要定期将数据保存在 csv 文件中该怎么办?
import bme680
import time
import datetime
from datetime import datetime
from bme680 import BME680
from pms5003 import PMS5003, ReadTimeoutError
try:
sensor = bme680.BME680(bme680.I2C_ADDR_PRIMARY)
except IOError:
sensor = bme680.BME680(bme680.I2C_ADDR_SECONDARY)
pms5003 = PMS5003()
readings = pms5003.read()
sensor.set_humidity_oversample(bme680.OS_2X)
sensor.set_pressure_oversample(bme680.OS_4X)
sensor.set_temperature_oversample(bme680.OS_8X)
sensor.set_filter(bme680.FILTER_SIZE_3)
print('Data sensing')
try:
while True:
if sensor.get_sensor_data():
output = datetime.now().strftime('%Y-%m-%d,%H:%M:%S,')+'{0:.2f} C,{1:.2f} hPa,{2:.3f} %RH'.format(
sensor.data.temperature,
sensor.data.pressure,
sensor.data.humidity)
if pms5003.read():
print(output, readings)
else:
print(output)
time.sleep(60)
except KeyboardInterrupt:
pass
我希望程序将数据保存在带有日期、时间、温度、湿度等标题的 csv 文件中。