gpt4 book ai didi

html - 在R中将字符转换为html

转载 作者:技术小花猫 更新时间:2023-10-29 12:12:12 25 4
gpt4 key购买 nike

在 R 中将包含非 ASCII 字符的字符(矢量)转换为 html 的首选方法是什么?例如,我想转换

  "ü"

  "ü"

我知道这可以通过巧妙地使用 gsub 来实现(但是有人一劳永逸地做到了吗?)而且我认为 R2HTML 包可以做到这一点,但事实并非如此。

编辑:这是我最终使用的;它显然可以通过修改字典来扩展:

char2html <- function(x){
dictionary <- data.frame(
symbol = c("ä","ö","ü","Ä", "Ö", "Ü", "ß"),
html = c("&auml;","&ouml;", "&uuml;","&Auml;",
"&Ouml;", "&Uuml;","&szlig;"))
for(i in 1:dim(dictionary)[1]){
x <- gsub(dictionary$symbol[i],dictionary$html[i],x)
}
x
}

x <- c("Buschwindröschen", "Weißdorn")
char2html(x)

最佳答案

这个问题已经很老了,但我找不到任何直接的答案...所以我想出了这个使用数字 html 代码并适用于 LATIN 1 - Supplement 的简单函数。 (整数值 161 到 255)。可能(当然?)在某些包中有一个函数可以更彻底地完成它,但接下来的内容可能对许多应用程序来说已经足够好了......

conv_latinsupp <- function(...) {
out <- character()
for (s in list(...)) {
splitted <- unlist(strsplit(s, ""))
intvalues <- utf8ToInt(enc2utf8(s))
pos_to_modify <- which(intvalues >=161 & intvalues <= 255)
splitted[pos_to_modify] <- paste0("&#0", intvalues[pos_to_modify], ";")
out <- c(out, paste0(splitted, collapse = ""))
}
out
}

conv_latinsupp("aeiou", "àéïôù12345")
## [1] "aeiou" "&#0224;&#0233;&#0239;&#0244;&#0249;12345"

关于html - 在R中将字符转换为html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13018570/

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