python-3.x - 将读取从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
解决方案
我认为您在阅读文件后没有输入“\n”。你只使用“\t”
推荐阅读
- ruby-on-rails - 如何一键更新Item?
- python - 如何获得numpy数组的原始组合?
- python - 请求获取保留响应 401
- docker - 将控制组规则应用于 Kubernetes 集群中的特定(或所有)Docker 容器
- python - 为什么在 Pycharm 中连接混合类型列表时会收到警告?
- javascript - 如何按顺序循环运行这些异步函数?
- react-native - React Native onLayout 与 React Hooks
- css - 剪辑路径 url 找不到 id
- java - 用于在映射 Informatica PowerCenter 中写入缺失值的 Java 代码
- json - 字典 JSON 的 SWIFT JSON解码器类型不匹配错误