我正在查看twitter数据,然后将这些数据输入到html文档中。通常,文本包含一些特殊字符,比如不能正确编码html的表情符号( emojis )。例如,tweet:
如果#复仇者结局和#小丑都被提名为最佳影片,这将是漫威对DC的第一次最佳影片比赛。我认为这两部电影都应该得到认可,但是在Twitter上的演讲将会是
。
将成为:
如果#复仇者结局和#小丑都被提名为最佳影片,这将是漫威对DC的第一次最佳影片比赛。我认为这两部电影都应该得到认可,但是在Twitter上的演讲将会是Ÿ“原Ÿ”元Ÿ“元”元。
当输入到html文档时。
我可以使用像https://www.textfixer.com/html/html-character-encoding.php这样的工具对tweet进行编码,如下所示:
如果#复仇者结局和#小丑都被提名为最佳影片,这将是漫威对DC的第一次最佳影片比赛。我认为这两部电影都应该得到认可,但在颁奖典礼前的推特讨论将是"�";"�";
然后,我可以将其输入到html文档中,并显示表情符号。在R中是否有一个包或函数可以接受文本和html编码,类似于上面的web工具?
发布于 2019-11-18 17:28:42
下面是一个函数,它将非ascii字符编码为HTML实体。
entity_encode <- function(x) {
cp <- utf8ToInt(x)
rr <- vector("character", length(cp))
ucp <- cp>128
rr[ucp] <- paste0("&#", as.character(cp[ucp]), ";")
rr[!ucp] <- sapply(cp[!ucp], function(z) rawToChar(as.raw(z)))
paste0(rr, collapse="")
}
这会返回
[1] "If both #AvengersEndgame and #Joker are nominated for Best Picture, it will be Marvel vs DC for the first time in a Best Picture race. I think both films deserve the nod, but the Twitter discourse leading up to the ceremony will be 🔥 🔥 🔥"
为您的输入,但这些似乎是等价的编码。
https://stackoverflow.com/questions/58845511
复制相似问题