@appare

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

RaspberryPi 温湿度記録しcsvファイルに記録するが1行目にtate_time,temp,hdmtが表示されない

RaspberryPi4にて温度湿度を記録しcsvファイルで保存するようにしています。
プログラム上では1行目にdate_time,temp,hdmtを表示するようにしているつもりですが
csvファイルを開くとうまくできていません。

qiita.rb
import RPi.GPIO as GPIO
import dht11
import datetime
import os

fileName='/home/pi/rasptemp//temp_hmdt.csv'
tempGpio=4

GPIO.setmode(GPIO.BCM)
dhtStat=dht11.DHT11(pin=tempGpio)

if not os.path.exists(fileName):
    thData=opne(fileName,'w')
    thData.write('date_time,temp,hdmt\n')
    thData.close()

while True:
    stat=dhtStat.read()
    now=str(datetime.datetime.now())[0:16]
    data=[now,stat.temperature,stat.humidity]
    if stat.temperature==stat.humidity==0:
        continue
    thData=open(fileName,'a')
    thData.write(','.join(map(str,data))+'\n')
    thData.close()
    break

コードは本を丸写しなのでできるはずなのですが
皆様のお力を貸していただけると助かります。

よろしくお願いします。

0 likes

2Answer

パット見る限り、以下のコードがopneとなっているのが気になりますが、関係ないですよね。
pythonあまり詳しくないのですが、opneとなっているサイトも見つかるので、どちらも許される?

if not os.path.exists(fileName):
    thData=opne(fileName,'w')

後、ファイルがすでにある場合にはヘッダは記載しないようになっているので、そのあたりとかも気になります。

0Like

Comments

  1. @appare

    Questioner

    返事が遅くなりました、ご指摘ありがとうございます。
    うまくいきました。

Comments

  1. @appare

    Questioner

    返事が遅くなりました、ご指摘ありがとうございます。

Your answer might help someone💌