【问题标题】:Removing unicode <U+????> from all observations in a column [duplicate]从列中的所有观察中删除 unicode <U+????> [重复]
【发布时间】:2017-10-16 13:09:24
【问题描述】:

我在一列中有一个 twitter 推文的数据框,在整个文本中都有各种 unicode。即不是在开头或结尾,而是在整个过程中随机出现。我只想从 text 列中删除所有 Unicode 并保留数据框。例如,如果一个观察结果是:text text &lt;U+FFH5&gt; text text &lt;U+301F&gt; text 我希望它返回:text text text text text

我已经尝试过:

twitter <- str_replace_all(twitter,"<U+[[:alnum:]]>","") 

twitter <- gsub("\\s*<U\\+\\w+>$","",twitter)

还有:

twitter$text <- str_replace_all(twitter$text,"<U+[[:alnum:]]>","") 

twitter$text <- gsub("\\s*<U\\+\\w+>$","",twitter$text)

它们不保留数据框。

我的数据框目前看起来像:

id    text
AA    Some text<U+FFFD>with some <U+671F> done
HH    <U+3010><U+5B9A><U+671F>good news
AA    Something<U+FFFD><U+FFFD>and so on
BB    Nothing at <U+3011>
AA    more<U+30C8>example

我想转换成:

id    text
AA    Some text with some  done
HH    good news
AA    Something and so on
BB    Nothing at
AA    more example

提前感谢您的帮助。

【问题讨论】:

  • 请提供reproducible example。您确定字符串“”在文本中是字面意思吗?或者您是否正在使用转义非 ascii 字符以使其可打印的查看器。
  • 您使用的是哪个客户端?那几乎可以肯定是一个不需要替换的 Unicode 字符。您以这种方式看到它的唯一原因是客户端无法正确显示非 ASCII 字符,或者设置指定显示编码值而不是字符本身。例如,R Studio 需要配置为使用 UTF8 来读取/写入显示
  • 另一个警告 - U+FFFD 对应于 Unicode 替换字符。当代码尝试使用另一个不兼容的代码页加载存储在一个代码页中的文本时,它会出现。该数据丢失。这段文字来自哪里?它是如何生成、保存、读取的?

标签: r string


【解决方案1】:

大概是这样的(部分基于Remove all text between two brackets):

twitter ="text <> text <U+FFH5> text text <U+301F> text"

str_replace_all(twitter,"\\<U[^\\>]*\\>","") # only removes unicode

【讨论】:

  • 我只是试图将它应用到我的数据框 twitter' as: twitter ]*\\>","")` 和它将我的数据框缩减为Large character (2 elements, 3.5 Mb)
  • 正如cmets中有人提到的,你确定这个字符序列存在吗?这不是 R 呈现 single Unicode 字符> 的方式
  • 不确定您是如何尝试替换这些值的。你在换吗twitter$text = str_replace_all(twitter$text,"\\&lt;U[^\\&gt;]*\\&gt;","")
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-22
  • 2017-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多