我有一个 mydata.txt/.json 文件,其中包含如下数据:
[{"num":1,"name":"Swab Summer: Transformation At the United States Coast Guard Academy","link":"http:\/\/www.amazon.com\/dp\/0982168594\/ref=wl_it_dp_v_nS_ttl\/176-1400914-4673658?_encoding=UTF8&colid=1GM97SGAP8NLI&coliid=I1ELS7DSQ6QV5C","old-price":"N\/A","new-price":"","date-added":"January 10, 2014","priority":"","rating":"N\/A","total-ratings":"","comment":"","picture":"http:\/\/ecx.images-amazon.com\/images\/I\/51MtOOm493L._SL500_SL135_.jpg","page":1}]
[{"num":1,"name":"Vibomex","link":"http:\/\/www.amazon.com\/dp\/B00BR1CUFY\/ref=wl_it_dp_v_S_ttl\/175-5687209-2417046?_encoding=UTF8&colid=C0XVZ38E5WD9&coliid=I1EPDGRY73N5Q2","old-price":"N\/A","new-price":"","date-added":"July 20, 2014","priority":"","rating":"N\/A","total-ratings":"","comment":"","picture":"http:\/\/ecx.images-amazon.com\/images\/I\/31GBqOHskyL._SL500_SL135_.jpg","page":1}]
基本上,多个 json 文件。这是两个独立的行。现在,当我尝试将数据导入 R 并将其设为数据帧时,它仅读取与第一行对应的行。下面是我的代码:
library(rjson)
json_file <- fromJSON(file="mydata.txt")
json_file <- lapply(json_file, function(x) {
x[sapply(x, is.null)] <- NA
unlist(x)
})
do.call("rbind", json_file)
任何人都可以帮助我读取 R 或 python 中单个数据帧中与 mydata.txt/json 的每一行相对应的行。如有任何帮助,我们将不胜感激!
最佳答案
这是一种方法,使用 jsonlite
包中提供的 fromJSON
:
do.call(rbind, lapply(readLines('mydata.json'), jsonlite::fromJSON))
# num name link
# 1 1 Swab Summer: Transformation At the United States Coast Guard Academy http://www.amazon.com/dp/0982168594/ref=wl_it_dp_v_nS_ttl/176-1400914-4673658?_encoding=UTF8&colid=1GM97SGAP8NLI&coliid=I1ELS7DSQ6QV5C
# 2 1 Vibomex http://www.amazon.com/dp/B00BR1CUFY/ref=wl_it_dp_v_S_ttl/175-5687209-2417046?_encoding=UTF8&colid=C0XVZ38E5WD9&coliid=I1EPDGRY73N5Q2
# old-price new-price date-added priority rating total-ratings comment picture page
# 1 N/A January 10, 2014 N/A http://ecx.images-amazon.com/images/I/51MtOOm493L._SL500_SL135_.jpg 1
# 2 N/A July 20, 2014 N/A http://ecx.images-amazon.com/images/I/31GBqOHskyL._SL500_SL135_.jpg 1
如果 json 文件中的列名称集有所不同,您可以使用:
library(dplyr)
rbind_all(lapply(readLines('mydata.json'), jsonlite::fromJSON))
关于python - R 或 Python 中多个 json 文件的数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26992262/