How to replace “unexpected escaped character” in R

我怕爱的太早我们不能终老 提交于 2019-12-12 11:10:17

问题


When I try to parse JSON from the character object from a Facebook URL I got "Error in fromJSON(data) : unexpected escaped character '\o' at pos 130". Check this out:

library(RCurl)
library(rjson)
data <- getURL("https://graph.facebook.com/search?q=multishow&type=post&limit=1500", cainfo="cacert.perm")
fbData <- fromJSON(data)
Error in fromJSON(data) : unexpected escaped character '\o' at pos 130

#with RSONIO also error
> fbData <- fromJSON(data)
Erro em fromJSON(content, handler, default.size, depth, allowComments,  : 
invalid JSON input

Is there any way to replace this '\o' character before I try to parse JSON? I tried gsub but it didn't work (or i'm doing something wrong).

datafixed <- gsub('\o',' ',data)
Error: '\o' is an unrecognized escape sequence in string starting with "\o"

Can somebody hel me with this one? Thanks.


回答1:


You need to escape \ in your pattern.

Try

gsub('\\o',' ',data)



回答2:


You could do

fbData <- fromJSON(data,unexpected.escape = "keep")

you will see a warning

Warning message:
In fromJSON(individual_page, unexpected.escape = "keep") :
unexpected escaped character '\m' at pos 10. Keeping value.

if you want you can suppress the warning using

suppressWarnings(fromJSON(data,unexpected.escape = "keep"))

unexpected.escape : changed handling of unexpected escaped characters. Handling value should be one of "error", "skip", or "keep"; on unexpected characters issue an error, skip the character, or keep the character

You can find more details here - http://cran.r-project.org/web/packages/rjson/rjson.pdf



来源:https://stackoverflow.com/questions/15768116/how-to-replace-unexpected-escaped-character-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!