我正在尝试将此输出写入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。
基于gas的来源,您应该可以这样做
readings = str(readings).split('\n')
[这将首先将readings
转换为字符串,然后将其拆分为行的列表,可以在for循环中对其进行迭代。