提取损坏的字符串

发布于 2025-02-06 09:03:40 字数 356 浏览 1 评论 0原文

我收到了一个怪异编码的文件,想知道是否有任何办法 检查“损坏”字符串。例如,

dat <- c("天脊煤化工集团股份有é\231\220å…¬å\217¸", "AB \"\"Achema\"\"", 
         "Abu Qir Fertilizers & Chemical", "Abu Zaabal Fertilizer &", 
         "ADP - Adubos De Portugal SA")

上面向量中的1和2元素被损坏,因为它们中有字符串和逃脱字符。我如何在vector dat中过滤或生成损坏字符串的索引

I received a file that had a weird encoding and wondered if there's any way to
check for 'corrupted' strings. For e.g.

dat <- c("天脊煤化工集团股份有é\231\220å…¬å\217¸", "AB \"\"Achema\"\"", 
         "Abu Qir Fertilizers & Chemical", "Abu Zaabal Fertilizer &", 
         "ADP - Adubos De Portugal SA")

The 1 and 2 element in above vector are corrupted since they have strings and escape characters in them. How can I filter these out or generate an index of corrupted strings in the vector dat

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叹倦 2025-02-13 09:03:40
error_string_idx <- which(
  is.na(
    iconv(
      dat,
      to = "ascii"
    ) 
  ) | grepl('\\\\|\\"', dat)
)
error_string_idx <- which(
  is.na(
    iconv(
      dat,
      to = "ascii"
    ) 
  ) | grepl('\\\\|\\"', dat)
)
海之角 2025-02-13 09:03:40

请尝试此尝试

gsub("[^a-zA-Z]" , "" , dat)

如果您不想空的角色使用,

Filter(function(x) nchar(x) , gsub("[^a-zA-Z]" , "" , dat))

Try this

gsub("[^a-zA-Z]" , "" , dat)

if you don't want empty character use

Filter(function(x) nchar(x) , gsub("[^a-zA-Z]" , "" , dat))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文