r - 如何在 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)
解决方案
如果它是文件中的单个 JSON,则
jsonlite::read_json("file1.txt")
# $id
# [1] 53680
# $title
# [1] "daytona1-usa"
如果是 NDJSON ( N
ewline- D
elimited json),那么
jsonlite::stream_in(file("file1.txt"), verbose = FALSE)
# id title
# 1 53680 daytona1-usa
推荐阅读
- c# - CS2066 无法将类型“OpenQA.Selenium.Chrome.Chromedriver”隐式转换为“OpenQA.Selenium.IWebDriver”。存在显式转换
- laravel - Laravel 8:如何在 server.php 中更改请求 uri
- r - 在 Shiny 中渲染饼图和渲染数据表之间制作可点击事件
- nginx - 如何通过域名访问redis服务器?
- javascript - 如何用多个html元素替换单个html元素
- java - 在对象列表中使用方法 Cast 而不是 Lambda
- flutter - 如何在 Dart 中将 base64 字符串 url 解码或转换为 UintList?
- authentication - Azure AD 身份验证和授权
- javascript - 至少两个输入字段的验证应填写 6 个或更多
- android - Kotlin Enum 类条目名称与正则表达式不匹配 - Android Studio