为 stri_unescape_unicode 将“\x”转换为“\x”以读取 R 中的字符串

问题描述 投票:0回答:1

我有一组名称,我需要将它们从当前的字符串形式转换为正确的图形重音。一个例子是:

"\xf3"

我正在尝试将此字符串替换为:

"\\xf3"

所以当我跑步时

stri_unescape_unicode("\\xf3")

我收到:

"ó"

我试过:

gsub("\x","\\x","\xf3")
Error: '\x' used without hex digits in character string starting ""\x"

然而,

gsub
陷入了十六进制数字和错误。

r string hex
1个回答
0
投票

你确实注意到需要替换任何东西,因为字符串

\
中没有
\xf3
。您可以使用
cat('\xf3')
,您会注意到这是一个多字节字符串。你只需要改变编码结构:

在基础 R 中使用:

iconv("\xf3", "latin1")
[1] "ó"

如果你坚持使用

stringi
那么:

stringi::stri_unescape_unicode(encodeString("\xf3"))
[1] "ó"
© www.soinside.com 2019 - 2024. All rights reserved.