从未知(对我来说)编码中删除不可打印的空格

时间:2022-03-05 19:20:28

So I've parsed some XML file using the XML package using the following code

因此,我使用以下代码使用r XML包解析了一些XML文件

library(XML) 
data <- xmlToDataFrame(xmlParse("Some file I can't share.xml"))

Everything worked fine and I've got the expected result

一切都很顺利,我得到了预期的结果

dim(data)
## [1] 554560 13

The only problem though that some of my entries looks as follows

唯一的问题是,我的一些条目如下所示

x <- "2  irfl014"
x
## [1] "2  \002\003\004\003\005\005\006\005\002\003\004\003\005\005\006\005irfl014"

Tried to identify the encoding (with no success)

试图识别编码(没有成功)

Encoding(x)
## [1] "unknown"

library(stringi)
stri_enc_detect(x)
# [[1]]
# [[1]]$Encoding
# [1] "UTF-8"     "Shift_JIS" "GB18030"   "EUC-JP"    "EUC-KR"    "Big5"     
# 
# [[1]]$Language
# [1] ""   "ja" "zh" "ja" "ko" "zh"
# 
# [[1]]$Confidence
# [1] 0.1 0.1 0.1 0.1 0.1 0.1

Encoding isn't my strongest field of expertise, is there any simple way to convert x to simply

编码并不是我最擅长的领域,有没有一种简单的方法可以将x转换成简单的。

x
## [1] "2  irfl014"

1 个解决方案

#1


10  

x <- "2  \002\003\004\003\005\005\006\005\002\003\004\003\005\005\006\005irfl014"

cat(x)
# 2  irfl014

The special characters, e.g., "\002" are non-printable control characters. See here for more information.

特殊字符,如“\002”是不可打印的控制字符。更多信息请参见这里。

You can use the following gsub command to remove all control characters:

可以使用下面的gsub命令删除所有控制字符:

gsub("[[:cntrl:]]+", "", x)
# [1] "2  irfl014"

#1


10  

x <- "2  \002\003\004\003\005\005\006\005\002\003\004\003\005\005\006\005irfl014"

cat(x)
# 2  irfl014

The special characters, e.g., "\002" are non-printable control characters. See here for more information.

特殊字符,如“\002”是不可打印的控制字符。更多信息请参见这里。

You can use the following gsub command to remove all control characters:

可以使用下面的gsub命令删除所有控制字符:

gsub("[[:cntrl:]]+", "", x)
# [1] "2  irfl014"