我有一个换行符分隔(即每个 JSON 对象仅限于文件中的 1 行):
{"name": "json1"}
{"name": "json2"}
{"name": "json3"}
在Python中,我可以轻松地阅读它,如下所示(我必须使用编码encoding='cp850'
读取我的真实数据):
import json
objs = []
with open("testfile.json", encoding='cp850') as f:
for line in f:
objs.append(json.loads(line))
我怎样才能在 R 中做类似的事情呢?
最后我想得到一个data.frame:
library("jsonlite")
library("data.table")
d <- fromJSON("testfile.json", flatten=FALSE)
df <- as.data.frame(d)
我们可以用stream_in
from jsonlite
library(jsonlite)
out <- stream_in(file('testfile.json'))
out
# name
#1 json1
#2 json2
#3 json3
str(out)
#'data.frame': 3 obs. of 1 variable:
#$ name: chr "json1" "json2" "json3"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)