如何在R中导入jsonl文件以及如何在csv中对其进行转换? [英] How do I import a jsonl file in R and how do I transform it in csv?

查看:98
本文介绍了如何在R中导入jsonl文件以及如何在csv中对其进行转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的PhD项目,我需要将JSONL文件读入R(扩展名不是json,是jsonl),然后将其转换为csv.我试图使用基于jsonlite的代码,但它给了我一个错误:

For my PhD project I need to read a JSONL file into R (the extension isn't json, is jsonl) and transform it in a csv. I tried to use this code based on jsonlite but it gives me an error:

library(jsonlite)
data <- "/Users/chiarap/tweets.jsonl"
dat <- fromJSON(sprintf("[%s]", paste(readLines(data), collapse=",")))
Error: parse error: unallowed token at this point in JSON text
          EFEF","notifications":null}},,,{"in_reply_to_status_id_str":
                     (right here) ------^

谢谢

推荐答案

如果文件很大,将所有行粘贴在一起可能会导致错误.您可以分别处理每行,然后将它们组合到一个数据框中.

If you have a large file, pasting all of the rows together may result in errors. You can process each line separately and then combine them into a data frame.

library(jsonlite)
library(dplyr)

lines <- readLines("myfile.jsonl")
lines <- lapply(lines, fromJSON)
lines <- lapply(lines, unlist)
x <- bind_rows(lines)

然后x是一个数据框,您可以继续使用它或将其写入文件.

Then x is a data frame that you can continue to work with or write to file.

这篇关于如何在R中导入jsonl文件以及如何在csv中对其进行转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆