gpt4 book ai didi

regex - 使用 str replace 删除 json 文档中的部分字符串用于许多记录

转载 作者:行者123 更新时间:2023-12-01 05:04:00 25 4
gpt4 key购买 nike

我想替换此文件中导致无效 json 参数的字符串。我可以手动删除第一个字符串 "_id": ObjectId( "539163d7bd350003"),并且可以将此 json 转换为数据框。有没有办法可以用str_replace之类的函数替换json文件的所有实例。我尝试了以下但无法使其工作。有什么建议?

library(RJSONIO)
library(stringr)

json_file<- '{ "_id" : ObjectId( "539163d7bd350003" ), "login" : "vui", "id" : 369607, "avatar_url" : "https://avatars.mashupsusercontent.com/u/369607?", "gravatar_id" : "df8897ffebe16c5b0cd690925c63e190", "url" : "https://api.mashups.com/users/vui", "html_url" : "https://mashups.com/vui", "followers_url" : "https://api.mashups.com/users/vui/followers", "following_url" : "https://api.mashups.com/users/vui/following{/other_user}", "gists_url" : "https://api.mashups.com/users/vui/gists{/gist_id}", "starred_url" : "https://api.mashups.com/users/vui/starred{/owner}{/repo}", "subscriptions_url" : "https://api.mashups.com/users/vui/subscriptions", "organizations_url" : "https://api.mashups.com/users/vui/orgs", "repos_url" : "https://api.mashups.com/users/vui/repos", "events_url" : "https://api.mashups.com/users/vui/events{/privacy}", "received_events_url" : "https://api.mashups.com/users/vui/received_events", "type" : "User", "site_admin" : false, "org" : "amurath" }'

str_replace(json_file,"_id*" , "")
json_file <- fromJSON(json_file)


json_file <- lapply(json_file, function(x) {
x[sapply(x, is.null)] <- NA
unlist(x)
})

df<- do.call("rbind", json_file)
df<- data.frame(json_file)

最佳答案

您可以使用以下内容:

str_replace(json_file,"\"_id[^,]*,\\s*" , "")

DEMO

关于regex - 使用 str replace 删除 json 文档中的部分字符串用于许多记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30789823/

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