yfgeek / rpi-TempRuntime

基于树莓派3B,DHT11/DHT22,LCD1602的一个实时温度湿度检测系统
118 stars 28 forks source link

python3支持不好怎么办(自问自答) #10

Open wjb711 opened 5 years ago

wjb711 commented 5 years ago

重构了json文件的写法, 希望解决大家python3下的不兼容问题 def writejson():

记录历史,写json文件的方法

try:

    with open('/var/www/html/rpi-TempRuntime/web/data/min/'+date+".json","r") as f:

        load_dict = json.load(f)

except:    

    load_dict=[]

dict0={}

dict0['tmp']=str(tmp)

dict0['hmt']=str(hmt)

dict0['time']=str(time)

load_dict.append(dict0)

with open('/var/www/html/rpi-TempRuntime/web/data/min/'+date+".json","w") as f:

    json.dump(load_dict,f)
liangbowang commented 4 years ago

出现这个报错,请问怎样改? raceback (most recent call last): File "/home/pi/bme280_1.py", line 35, in jsonWrite(i,todaytime,'min') File "/home/pi/bme280_1.py", line 20, in jsonWrite data['tmp']=str(tmp) NameError: name 'tmp' is not defined

wjb711 commented 4 years ago

我试着答复下,提示tmp未定义,tmp源码里应该是温度,你看下源码里温度如何获取的,把tmp定一下就好

---原始邮件--- 发件人: "liangbowang"<notifications@github.com> 发送时间: 2019年9月14日(星期六) 下午5:42 收件人: "yfgeek/rpi-TempRuntime"<rpi-TempRuntime@noreply.github.com>; 抄送: "opencv_learner"<10054053@qq.com>;"Author"<author@noreply.github.com>; 主题: Re: [yfgeek/rpi-TempRuntime] python3支持不好怎么办(自问自答) (#10)

出现这个报错,请问怎样改? raceback (most recent call last): File "/home/pi/bme280_1.py", line 35, in jsonWrite(i,todaytime,'min') File "/home/pi/bme280_1.py", line 20, in jsonWrite data['tmp']=str(tmp) NameError: name 'tmp' is not defined

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

liangbowang commented 4 years ago

谢谢,这个问题已解决了。 我是初学者,还有两个问题请帮忙看看,json文件里新的数据不能append,每次都是清空了上一次的数据,然后加入一组新数据,所以json文件里总是只有一组数据。

另外:json数据里的时间总是这样子的,"time": "<module 'time' (built-in)>"}

wjb711 commented 4 years ago

你用的是python3吧,好像源代码是在2里测试的,3里有问题。 我重构了3里writejson部分 你参考下 https://github.com/wjb711/Pi_environment/blob/master/DHT22_LCD2004.py

---原始邮件--- 发件人: "liangbowang"<notifications@github.com> 发送时间: 2019年9月14日(星期六) 晚上6:26 收件人: "yfgeek/rpi-TempRuntime"<rpi-TempRuntime@noreply.github.com>; 抄送: "opencv_learner"<10054053@qq.com>;"Author"<author@noreply.github.com>; 主题: Re: [yfgeek/rpi-TempRuntime] python3支持不好怎么办(自问自答) (#10)

谢谢,这个问题已解决了。 我是初学者,还有两个问题请帮忙看看,json文件里新的数据不能append,每次都是清空了上一次的数据,然后加入一组新数据,所以json文件里总是只有一组数据。

另外:json数据里的时间总是这样子的,"time": "<module 'time' (built-in)>"}

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.