0
我必须从推文中清除特殊字符,例如👉👌💦✨
。为了做到这一点,我遵循了这一策略(我使用Python 3):如何从推文中删除特殊字符(如`'ŒðŸ'`)
- 从字节转换鸣叫字符串以获得特殊字符为十六进制,所以
Ã
成为\xc3\
; - 使用正则表达式,删除
b'
和b"
(在字符串的开头)和'
或"
(在字符串的末尾)的转换处理之后被Python加入; - 最后删除十六进制表示,也使用正则表达式。
这里是我的代码:
import re
tweet = 'b"[/Very seldom~ will someone enter your life] to question\xc3\xa2\xe2\x82\xac\xc2\xa6 "'
#encoding to 'utf8'
tweet_en = tweet.encode('utf8')
#converting to string
tweet_str = str(tweet_en)
#eliminating the b' and b" at the begining of the string:
tweet_nob = re.sub(r'^(b\'b\")', '', tweet_str)
#deleting the single or double quotation marks at the end of the string:
tweet_noendquot = re.sub(r'\'\"$', '', tweet_nob)
#deleting hex
tweet_regex = re.sub(r'\\x[a-f0-9]{2,}', '', tweet_noendquot)
print('this is tweet_regex: ', tweet_regex)
最终输出是:[/Very seldom~ will someone enter your life] to question "
(从中我仍然无法删除最后"
)。我想知道是否有更好更直接的方式来清除Twitter数据中的特殊字符。任何帮助将不胜感激。