我如何将此循环的输出写入脚本

问题描述 投票:0回答:1

我正在尝试将此输出写入csv,但出现错误。

import csv

gas.enable_adc()
gas.set_adc_gain(4.096)

try:
    while True:
       readings = gas.read_all()
       logging.info(readings)
       time.sleep(1.0)

    print(readings)

    #These are the reading that I want to write to csv
    #Oxidising: 19768.76 Ohms
    #Reducing: 269925.93 Ohms
    #NH3: 368827.59 Ohms
    #ADC: 00.62 Volts

    with open('gas1.csv', 'w') as csvfile:
        csv.writer(csvfile)
        for x in readings:
            csvfile.writerow(x + ',' + '/n')

except KeyboardInterrupt:
    pass

这是我得到的错误:

。Traceback(最近通话):。文件“ /home/pi/Documents/gas1.py”,第33行,在.for x在读数中:.TypeError:“ items6814Reading”对象不是iterabl。

python csv
1个回答
0
投票

基于gas的来源,您应该可以这样做

readings = str(readings).split('\n')

[这将首先将readings转换为字符串,然后将其拆分为行的列表,可以在for循环中对其进行迭代。

© www.soinside.com 2019 - 2024. All rights reserved.