我必须清除tweets中的特殊字符,如👉👌💦✨
。为此,我遵循以下策略(我使用Python 3):
Ã
变成{b'
和b"
(在字符串开头)和'
或{这是我的代码:
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数据中的特殊字符。任何帮助都将不胜感激。在
如果您只需要查找ASCII字符,我认为这会很好地工作:
您可以执行
ord(c) in range()
,并给它一个要保留的文本范围(可能包括表情符号)。在相关问题 更多 >
编程相关推荐