从ina219读取读数到文件

时间:2019-07-16 09:19:56

标签: python-3.x csv

我想使用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

1 个答案:

答案 0 :(得分:0)

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