【问题标题】:How do I remove \uXXXX from a list of strings? [duplicate]如何从字符串列表中删除 \uXXXX? [复制]
【发布时间】:2020-08-13 16:23:22
【问题描述】:

我想删除所有以 \u 开头的单词。我相信这些是 unicode '\uXXXX'。

原始字符串:

"RT  \u2066als \u2066@WBHoekstra\u2069 zijn poot maar stijf houdt in de Italiaanse kwestie. Leest Mattheus 25, 2-13 '"

期望的输出:

"RT @WBHoekstra zijn poot maar stijf houdt in de Italiaanse kwestie. Leest Mattheus 25, 2-13 '"

我尝试像这样使用正则表达式:

re.sub('\u\w+','',item )

但我收到以下错误:

"SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \uXXXX escape"

【问题讨论】:

标签: python regex string unicode python-unicode


【解决方案1】:

你可以使用.encode('ascii', 'ignore')来做到这一点

"RT  \u2066als \u2066@WBHoekstra\u2069 zijn poot maar stijf houdt in de Italiaanse kwestie. Leest Mattheus 25, 2-13 '".encode('ascii', 'ignore')

输出

 b"RT  als @WBHoekstra zijn poot maar stijf houdt in de Italiaanse kwestie. Leest Mattheus 25, 2-13 '"

【讨论】:

  • 这行得通,谢谢。但是,是否有可能删除字符串开头的“b”?我正在预处理一些推文,但我不能在其中包含“b”。我刚刚尝试使用正则表达式删除它,但它不起作用。
猜你喜欢
  • 2015-09-06
  • 2014-01-26
  • 2017-04-18
  • 2014-02-15
  • 2019-02-12
  • 2019-06-01
  • 2014-08-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多