【问题标题】:Replace UTF-8 encoded Twitter emojis in python在 python 中替换 UTF-8 编码的 Twitter 表情符号
【发布时间】:2019-02-26 13:11:58
【问题描述】:

我有一组编码为 UTF-8 的推文。数据被加载到熊猫数据报中。这也包括 Twitter 表情符号(例如:\xf0\x9f\x8e\xb5)。我想在 python 中使用正则表达式用空格或空字符替换这些表情符号。以下是我尝试的方法。但我的正则表达式模式标记为红色,我收到警告。如何删除这些 Twitter 表情符号?

>>> preparedData.head(5).to_dict()
{'id': {0: 1042616899408945154, 1: 1042592536769044487, 2: 1042587702040903680, 3: 1042587263643930626, 4: 1042586780292276230}, 'date': {0: '2018-09-20', 1: '2018-09-20', 2: '2018-09-20', 3: '2018-09-20', 4: '2018-09-20'}, 'time': {0: '03:30:14', 1: '01:53:25', 2: '01:34:13', 3: '01:32:28', 4: '01:30:33'}, 'text': {0: "b'\\xf0\\x9f\\x8c\\xb9 are red, violets are blue, if you want to buy us \\xf0\\x9f\\x92\\x90, here is a CLUE \\xf0\\x9f\\x98\\x89 Our #flowerpowered eye & cheek palette is AL\\xe2\\x80\\xa6 ", 1: "b'\\xf0\\x9f\\x8e\\xb5Is it too late now to say sorry\\xf0\\x9f\\x8e\\xb5 #tartetalk #memes ", 2: "b'@JillianJChase Oh no! Please email your order # to social@tarte.com & we can help \\xf0\\x9f\\x92\\x95'", 3: 'b"@Danikins__ It\'s best applied with our buffer brush! \\xf0\\x9f\\x92\\x9c\\xc2\\xa0', 4: "b'@AdelaineMorin DEAD \\xf0\\x9f\\xa4\\xa3\\xf0\\x9f\\xa4\\xa3\\xf0\\x9f\\xa4\\xa3'"}, 'hasMedia': {0: 0, 1: 1, 2: 0, 3: 0, 4: 0}, 'hasHashtag': {0: 1, 1: 1, 2: 0, 3: 0, 4: 0}, 'followers_count': {0: 801745, 1: 801745, 2: 801745, 3: 801745, 4: 801745}, 'retweet_count': {0: 17, 1: 94, 2: 0, 3: 0, 4: 0}, 'favourite_count': {0: 181, 1: 408, 2: 0, 3: 0, 4: 14}}

我是如何尝试的

preparedData['url removed'] = re.sub(r''/[\x{1F600}-\x{1F64F}]/u'', ' ', preparedData['text'])
print(preparedData.head(5).to_dict())

Warning

This inspection detects names that should resolve but don't. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases. Top-level and class-level items are supported better than instance items

【问题讨论】:

标签: python regex


【解决方案1】:

可以使用django.utilssmart_str函数

from django.utils.encoding import smart_str,smart_unicode

cleaned_up_text=smart_str(your_text_with_encoding)

【讨论】:

  • 如果有帮助请告诉我。
猜你喜欢
  • 2016-06-10
  • 1970-01-01
  • 2016-11-04
  • 2018-07-19
  • 1970-01-01
  • 2015-08-15
  • 1970-01-01
  • 2016-03-28
  • 2022-07-23
相关资源
最近更新 更多