如何使用xlsxwriter写入具有多个变量的多个工作表

时间:2019-07-20 01:29:57

标签: python-3.x variables iteration xlsxwriter

我有一个项目,可以将多个与日期时间相关的温度读数写入Excel文件中的单独工作表中。我正在使用xlsxwriter和带有相应传感器名称及其序列号的字典。 我正在尝试让xlsxwriter为每个温度计单独记录一张纸的读数和相应的时间。

到目前为止,我可以用每个温度计的名称来命名表,但是我只能在最后一个表或第一个表上写读数。我怀疑这应该是双for循环,但我在任何地方都找不到答案。

dirName = 'Excel_log'
parser = ConfigParser()
parser.read('config.ini')
sensors = dict(parser.items('sensors and ids'))
time_now = time.asctime()
row = 0
col = 0
i = 0
sensorNames = list(sensors.values())


for sensor, key in sensors.items():
    worksheet = workbook.add_worksheet(key)
    row = 0
    col = 0
    w1sensor = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS18B20,sensor)
    worksheet.write(row,col, 'Date and time')
    worksheet.write(row,col + 1, 'Temperature')
    row +=1
for values in sensorNames:
    worksheet.write(row,col, time_now)
    worksheet.write(row,col + 1, w1sensor.get_temperature())
    row +=1
    worksheet.write(row,col, time_now)
    worksheet.write(row,col + 1, w1sensor.get_temperature())
    i +=1
    row +=1
    print('Temperature and date registered correctly', i ,'time(s)')
    time.sleep(5)
#for worksheet in workbook.worksheets():
workbook.close()
print('added : %d sheets' % len(sensors))

the dictionary is the following:
[sensors and ids]
021312e688aa = sensor1
021312d44daa = sensor2
01131be85214 = sensor3

0 个答案:

没有答案