【问题标题】:Removing part of string starting with \ud删除以 \ud 开头的部分字符串
【发布时间】:2021-08-23 12:13:22
【问题描述】:

我正在尝试删除以 \ud 开头的任何内容

My text:
onceuponadollhouse: "Iconic apart and better together \ud83d\udc6fâ€â™€ï¸The  Caboodles® x Barbieâ„¢ collection has us thinking about our Doll Code \ud83c\udf80 We stand for one another by sharing our lessons

The answer I am looking for:
onceuponadollhouse: "Iconic apart and better together â€â™€ï¸The  Caboodles® x Barbieâ„¢ collection has us thinking about our Doll Code We stand for one another by sharing our lessons

【问题讨论】:

标签: python r symbols emoji


【解决方案1】:

因此,理想的方法是退后一步,找出编码在处理过程中被破坏的地方,然后修复它。不知何故,您得到了 (a) 代理对,即以 \ud 开头的字符对; (b) UTF-8 被解释为 Latin-1 或一些类似的编码,例如“芭比娃娃”之后的 â„¢。

退后一步,确保您的输入文本被正确解释是理想的;在这里,您正在失去表情符号“兔耳朵的女人”和“丝带”;另一次可能是某人的名字或其他重要信息。


如果您遇到无法正确执行的情况,并且需要剥离代理对,您可以使用re.sub

import re

text = 'onceuponadollhouse: "Iconic apart and better together \ud83d\udc6fâ€â™€ï¸The  Caboodles® x Barbieâ„¢ collection has us thinking about our Doll Code \ud83c\udf80 We stand for one another by sharing our lessons'

stripped = re.sub('[\ud800-\udfff]+', '', text)

print(stripped)

根据您的目的,将这些字符替换为占位符可能会很有用;因为它们总是成对出现,你可以这样做:

import re

text = 'onceuponadollhouse: "Iconic apart and better together \ud83d\udc6fâ€â™€ï¸The  Caboodles® x Barbieâ„¢ collection has us thinking about our Doll Code \ud83c\udf80 We stand for one another by sharing our lessons'

stripped = re.sub('[\ud800-\udfff]{2}', '<unknown character>', text)

print(stripped)

【讨论】:

    【解决方案2】:

    查看emot python 包。我今天早上从这篇文章中发现了它:https://towardsdatascience.com/5-python-libraries-that-you-dont-know-but-you-should-fd6f810773a7

    文档中给出的示例仅解释和表情符号,但它也给出了它们的位置,因此替换它们不会太大。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-16
      • 1970-01-01
      • 2015-01-28
      • 2019-11-15
      • 2016-03-16
      • 2013-10-30
      • 1970-01-01
      相关资源
      最近更新 更多