首页 > 解决方案 > 将数据保存在列中 Python DS18B20

问题描述

我的 Python 数据代码保存了温度传感器(DS18B20)的数据,但没有将温度保存在列中。温度写在这样的一行中:['25.25']。有人可以告诉我如何解决它。我必须将传感器中读取的温度分成几个数字。谢谢你的帮助

    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/0 错误,有人可以帮忙吗?

标签: python

解决方案


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

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")

推荐阅读