首页 > 解决方案 > 如何在 R 中正确打开此文本文件?

问题描述

所以我在一个文件中有这行代码:

{"id":53680,"title":"daytona1-usa"}

但是当我尝试使用这个在 R 中打开它时:

df <- read.csv("file1.txt", strip.white = TRUE, sep = ":")

它产生这样的列:

Col1: X53680.title
Col2: daytona1.usa.url

我想要做的是打开文件,使列是这样的:

Col1: 53680
Col2: daytona1-usa

我怎样才能在 R 中做到这一点?

编辑:我正在阅读的实际文件是这样的:

{"id":53203,"title":"bbc-moment","url":"https:\/\/wow.bbc.com\/bbc-ids\/live\/enus\/211\/53203","type":"audio\/mpeg"},{"id":53204,"title":"shg-moment","url":"https:\/\/wow.shg.com\/shg-ids\/live\/enus\/212\/53204","type":"audio\/mpeg"},{"id":53205,"title":"was-zone","url":"https:\/\/wow.was.com\/was-ids\/live\/enus\/213\/53205","type":"audio\/mpeg"},{"id":53206,"title":"xx1-zone","url":"https:\/\/wow.xx1.com\/xx1-ids\/live\/enus\/214\/53206","type":"audio\/mpeg"},], WH.ge('zonemusicdiv-zonemusic'), {loop: true});

读完之后,我删除了第一列,然后删除了第三列和第四列:

# Delete the first column
df <- df[-1]

# Delete every 3rd and 4th columns
i1 <- rep(seq(3, ncol(df), 4) , each = 2) + 0:1
df <- df[,-i1]

谢谢你。

编辑2:

添加此修复它:

df[] <- lapply(df, gsub, pattern = ".title", replacement = "", fixed = TRUE)
df[] <- lapply(df, gsub, pattern = ",url", replacement = "", fixed = TRUE)

标签: r

解决方案


如果它是文件中的单个 JSON,则

jsonlite::read_json("file1.txt")
# $id
# [1] 53680
# $title
# [1] "daytona1-usa"

如果是 NDJSON ( Newline- Delimited json),那么

jsonlite::stream_in(file("file1.txt"), verbose = FALSE)
#      id        title
# 1 53680 daytona1-usa

推荐阅读