Convert .dbc files into .csv with python [closed]
Asked Answered
K

5

8

I'm writing a python script to convert .DBC files (from Datasus) into .CSV.

I've searched a lot some libs to help me but didn't get success with any of them. Do someone know an option/package which could be useful in this conversion task?

Knoll answered 13/2, 2019 at 1:26 Comment(0)
K
8

I really don't understand why some members give -1 to the question, but don't really answer it.

Well, I've not found out a good lib to convert .DBC to .CSV. But I could do it using Python + R and I'm sharing here how I achieved it.

The Python script below gets R path and executes the dbc2csv.R file sending three args: the raw file path, converted file path, and the file name that will be converted.

import subprocess
import commands


def dbc2csv(raw_filename):
dbc2csv_path = "/path/to/script/dbc2csv.R " + raw_files_dir + " " + converted_files_dir + " " + raw_filename

try:
    r_script_path = commands.getstatusoutput('which Rscript')[1]
    subprocess.call(r_script_path + " --vanilla " + dbc2csv_path, shell=True)
    return True
except:
    print("(Rscript) Error converting file: " + raw_filename)

return False

The R script dbc2csv.R bellow is actually who will convert.

#install.packages("read.dbc") You need this package
library("read.dbc")


dbc2dbf <- function(rawDir, convertedDir, file) {
    # reads dbc file
    x <- read.dbc(paste(rawDir, file, sep=""))
    # write it to csv
    write.csv(x, file=paste(convertedDir, file, ".csv", sep=""))
}

args = commandArgs(trailingOnly=TRUE)
try(dbc2dbf(args[1], args[2], args[3]), TRUE)

We do know it would be better if we could convert using just Python. But this approach will work fine.

Knoll answered 1/6, 2019 at 20:5 Comment(3)
I tried using this approach but Rstudio open but it doesn't run the scriptIntrauterine
Please send me your implementation: [email protected]. Already have R installed and R studio and python. All configured to work in command prompt. Windows 10.Intrauterine
@Intrauterine You can find in my Github github.com/LucasMMota/datasus-data-extractorKnoll
A
2

If you're using python on Linux, one of the alternatives is using the project located at this repository: https://github.com/greatjapa/dbc2csv

It works by creating a docker that runs the bash script followed by the python script (you can also run them separately, without using docker, and that will work just as well).

It's also worth noting that the .dbc extension used by Datasus is one that they created themselves (not related to the .dbc used by cantools and other such tools), which just helps it being a pain to work with.

Acetanilide answered 20/2, 2020 at 2:7 Comment(1)
thanks from bringing this contribution and the repositoryKnoll
B
2

In python there is this awesome library https://pypi.org/project/cantools/

You can go through the source code of it to find the fields you need to extract : https://github.com/eerimoq/cantools/tree/f120d35e1523201b6d87e87306457bf1769330e3

All relevant fields for messages and signals can be found out in cantools/database/can/message.py and /database/can/signal.py in this github link

For eg: if you need names and unit of signals you could use signal.name and signal.unit

Sample code to extract these fields:

 #**dbc_file** is the full path of dbc 
    db = cantools.database.load_file(dbc_file)
    for msg in db.messages:
        msg_name = msg.name
        msg_id = msg.frame_id
        msg_length = msg.length
        sender = msg.senders
        msg_group = db.get_message_by_name(msg_name)
    
        for signal in msg_group.signals:
            
            #name of signal
            sig_name = signal.name
            #unit of signal
            sig_unit = signal.unit

Hope this might help someone!!

Being answered 16/12, 2020 at 13:54 Comment(0)
M
0

I don't know of anything that does an actual conversion, but assuming you mean the DBC BUS format, there's a Python library to read that: https://pypi.org/project/cantools/

And Python has built-in tools for writing CSVs: https://docs.python.org/3/library/csv.html

It should take only a handful of lines to combine those libraries into a converter.

Mistreat answered 13/2, 2019 at 1:34 Comment(1)
I tried that but didn`t work on the DBC files from Datasus. I could achieve this when using an R script developed to read those files and combining this with python.Knoll
M
-1

Convert it to XLS (microsoft excel) and from there you can get it to .csv, I am ubuntu guy but I believe there should be a export option. or at least there is in LibreOffice

Mamiemamma answered 13/2, 2019 at 1:40 Comment(1)
This doesn`t help as the problem of converting from .DBC to any type continues due to the difficulty on finding good libraries to do that.Knoll

© 2022 - 2024 — McMap. All rights reserved.