Search code examples
pythonexport-to-csvobd-iiexport-to-text

How to export the data - Python


I want to export the real time data that I am taking from my car to a .txt or .csv file (or something else that I don't know). How can I do that?

Here's the documentation for 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()

Solution

  • If i understand correctly you can simply use:

    with open("output.txt", "a") as f:
        f.write(f"SPEED:{r.value}")
    

    This also automatically closes the file after using it (when the execution gets out of the with open() scope)