r JSON到CSV转换器。使用`jsonlite` R包,展平所有层次结构并将所有剩余的列表/数组转换为strin
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了r JSON到CSV转换器。使用`jsonlite` R包,展平所有层次结构并将所有剩余的列表/数组转换为strin相关的知识,希望对你有一定的参考价值。
#!/usr/bin/Rscript
if (!require(jsonlite, quietly = TRUE, warn.conflicts = FALSE)) {
stop("This program requires the `jsonlite` package. Please install it by running `install.packages('jsonlite')` in R and then try again.")
}
args <- commandArgs(trailingOnly = TRUE)
if (length(args) == 0) {
input <- readLines(file("stdin"))
} else if (args[[1]] == "--help") {
write("Usage: json2csv <json input file> <csv output file> \nAlternatively, you can redirect inputs and outputs using pipes, stdin and stdout.",
stderr())
q()
} else {
input <- readLines(args[[1]])
}
df <- fromJSON(input, flatten = TRUE)
listcols <- colnames(df[lapply(colnames(df), function(x) class(df[,x])) == "list"])
for (col in listcols) df[col] = data.frame(unlist(lapply(df[,col], function(x) toString(unlist(x)))))
if (length(args) > 1) {
write.csv(df, args[[2]], row.names = FALSE)
} else {
write.csv(df, stdout(), row.names = FALSE)
}
以上是关于r JSON到CSV转换器。使用`jsonlite` R包,展平所有层次结构并将所有剩余的列表/数组转换为strin的主要内容,如果未能解决你的问题,请参考以下文章
Python将csv文件转换为Json问题
Python爬虫编程思想(157):使用Scrapy从CSV格式转换到JSON格式
Python爬虫编程思想(157):使用Scrapy从CSV格式转换到JSON格式
Python爬虫编程思想(157):使用Scrapy从CSV格式转换到JSON格式
获取Gallup数据JSON页面并使用R转换为csv
python转换xsl到csv与整理json