gpt4 book ai didi

json - 大型 json 文件示例

转载 作者:行者123 更新时间:2023-12-04 13:17:46 26 4
gpt4 key购买 nike

我有一个很大的 JSON 文件(8 GB,800 万个案例),但我只需要它的一个小样本。简单 stream_in文件太大,不起作用。

为了解决这个问题,我尝试了以下代码:

books <- list("Books_5.json") 
books <- map(books, ~ stream_in(file(.x)) %>% sample_n(385))
books <- as.data.frame(books)

问题是在 300 万页后 R 停止读取文件,因为文件太大了。知道如何获得该文件的 385 个案例的样本吗?

较小文件的示例。变量是一样的。
Variables: 9
$ reviewerID <chr> "AF50PEUSO9MSV", "A1L0TVAJ1TYE06", "A64NRL5OSR3KB", ...
$ asin <chr> "B0000A1G05", "B009SQQF9C", "B005HRT88G", "B00D5T3QK...
$ reviewerName <chr> "Matthew J. Hodgkins", "TCG", "Me", "J. Lee", "A. Bu...
$ helpful <list> [<1, 1>, <0, 1>, <1, 1>, <0, 0>, <0, 0>, <0, 0>, <0...
$ reviewText <chr> "This is the lens that I always keep on my camera by...
$ overall <dbl> 5, 5, 5, 5, 5, 5, 5, 4, 5, 2, 5, 4, 5, 4, 5, 5, 3, 4...
$ summary <chr> "Great lens!", "I love them! What else can I say", "...
$ unixReviewTime <int> 1370736000, 1404518400, 1387411200, 1385769600, 1379...
$ reviewTime <chr> "06 9, 2013", "07 5, 2014", "12 19, 2013", "11 30, 2...

最佳答案

如果您的文件每行有一个案例,您可以使用 LaF打包以非常有效地执行此操作:

library(LaF)
random_lines <- sample_lines(filename = "Books_5.json",
n = 385)

然后,您可能需要将行转换为所需的格式,但此解决方案允许从大文件中读取数据,而无需将所有内容都读入内存。

编辑:
将结果行转换为 data.frame :
do.call("rbind",lapply(random_lines, fromJSON))

只要 JSON 对象中的字段只包含单个值(即不嵌套),这应该有效。

关于json - 大型 json 文件示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46808499/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com