[英]How to export the data - Python
我想將從我的車上獲取的實時數據導出到.txt
或.csv
文件(或其他我不知道的文件)。 我怎樣才能做到這一點?
這是obdII 的文檔。
import obd #import required libraries
import time
import serial
connection = obd.Async( fast= False) #auto-connects to USB or RF port
#RPM
def new_rpm(r):
print("RPM:",r.value)
print("\t")
#SPEED
def new_speed(r):
print("SPEED:",r.value)
print("\t")
**#Is it ok to do that with the watch or with the response ? **
connection.watch(obd.commands.RPM, callback=new_rpm)
connection.watch(obd.commands.SPEED, callback=new_speed)
connection.start()
#the callback now will be Enabled for new values
time.sleep(60)
connection.stop()
如果我理解正確,你可以簡單地使用:
with open("output.txt", "a") as f:
f.write(f"SPEED:{r.value}")
這也會在使用后自動關閉文件(當執行超出with open()
范圍時)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.