我正在编写一个python脚本,将.DBC
文件(从Datasus)转换为.CSV
。
我已经找了很多朋友来帮我,但是他们都没有成功。有人知道在这个转换任务中可能有用的选项/包吗?
发布于 2019-06-01 20:05:22
我真的不明白为什么有些成员对这个问题给予-1,但没有真正回答。
好吧,我还没有找到一个很好的库来将.DBC
转换为.CSV
。但是我可以使用Python +R实现它,我在这里分享我是如何实现它的。
下面的Python获取R路径并执行发送三个args的dbc2csv.R
文件:原始文件路径、转换的文件路径和将要转换的文件名。
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
R脚本dbc2csv.R
下面实际上是谁将转换。
#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)
我们确实知道,如果我们只使用Python进行转换会更好。但这种方法会很好。
https://stackoverflow.com/questions/54661137
复制相似问题