首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在列中保存数据Python DS18B20

在列中保存数据Python DS18B20
EN

Stack Overflow用户
提问于 2020-08-23 23:00:55
回答 1查看 101关注 0票数 0

我的Python数据代码保存温度传感器(DS18B20)的数据,但不会将温度保存在列中。温度是这样写的:'25.25‘。有没有人能告诉我怎么修。我必须将传感器的读数温度分成几个数字。谢谢你的帮忙

代码语言:javascript
复制
    while programmStatus == 1:
    #while True:
            now = datetime.now(timezoneBerlin)
                
#while True:
    # open new file
            with open ( 'test.tsv', 'w') as f:
                for temperature_single in Temperature:
                    f.write ("{:>5}\n".format('Temperature'))    
                    f.write("\t{:>5}\n".format 
                    (format(temperature_single)))
                    f.write("\n")
                    f.flush()
                    f.close()
   
                x = 0
                ds1820readout()
                print ("Sensorname und Temperaturevalue:")
                while x < tempSensorQuantity:
                    print (tempSensorName[x] , " " , tempSensorValue[x], " °C")
                    x = x + 1
print ("\n")

在这段代码中,关闭的文件有一个I/O错误,有没有人能帮忙?

EN

回答 1

Stack Overflow用户

发布于 2020-09-02 02:41:26

似乎温度是一个值列表。如果您尝试将它们放入csv中,则必须首先迭代列表,例如:

代码语言:javascript
复制
tempSensorName = []
tempSensorQuantity = 0 
tempSensorValue = [] 
programmStatus = 1
Temperature = tempSensorValue   

def ds1820einlesen():
    global tempSensorName, tempSensorQuantity, programmStatus 
def ds1820auslesen():
    global tempSensorName, tempSensorQuantity, tempSensorValue,              
            with open ( 'test.tsv', 'w') as f:
                for temperature_single in Temperature:
                    f.write ("{:>5}\n".format('Temperature'))    
                    f.write("\t{:>5}\n".format(format(temperature_single)))
                    f.write("\n")
                    f.flush()
                    f.close()
    
                    x = 0
                    ds1820auslesen()
                    print ("Sensorname and Temperaturevalue:")
                    while x < tempSensorQuantity:
                        print (tempSensorName[x] , " " ,
    tempSensorValue[x], " °C")
                            x = x + 1
    print ("\n")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63548459

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档