【发布时间】:2020-01-26 19:29:14
【问题描述】:
我有一个换行符分隔(即每个 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)
【问题讨论】:
-
你能显示
d的输出吗 -
试试
read_json或stream_in -
我无法显示
d,因为fromJSON失败并出现以下错误:Error: lexical error: invalid char in json text. testfile.json (right here) ------^ -
我会和
read_json一起阅读,看看它是否有效 (d <- read_json('testfile.json') -
我有一个换行符分隔(即每个 JSON 对象被限制在文件中的 1 行) 为什么?你有什么办法改变吗?