首页 > 解决方案 > 将读取从ina219传递到文件

问题描述

我想使用 ina219 将电流和电压记录到 CSV 文件中,python 程序应该在无限循环中运行,直到我们按 ctrl+c

这就是我尝试过的

def read():
    ina = INA219(SHUNT_OHMS)
    ina.configure()
    try:
        with open('loop.csv','w') as f1:
            writer=csv.writer(f1, delimiter='\t',lineterminator='\n',)
            row = '%.3f' % ina.voltage()+'\t'+'%.3f' % ina.current()+'\t'+'%.3f' % ina.power()+'\t'+'%.3f' % ina.shunt_voltage()
            writer.writerow(row)
    except DeviceRangeError as e:
        # Current out of device range with specified shunt resistor
        print(e)


if __name__ == "__main__":
    with open('loop.csv','w') as f1:
            writer=csv.writer(f1, delimiter='\t',lineterminator='\n',)
            row = 'Bus Voltage V'+'\t'+'Bus Current mA'+'\t'+'Power mW'+'\t'+'shunt_voltage mV'
            writer.writerow(row)
    while 1 :
        read()
        time.sleep(.300)

这就是我期望它从 loop.csv 得到的

Bus Voltage V   Bus Current mA   Power mw   Shunt voltage mv
2               3                1          2

但这就是我得到的

2   .   7   6   4   "   "   -   0   .   0   9   8   "   "   0   .   4   8   8   "   "   -   0   .   0   1   0

标签: python-3.xcsv

解决方案


我认为您在阅读文件后没有输入“\n”。你只使用“\t”


推荐阅读