【问题标题】:Problems with cleaning tweet (emoticons, smileys ...)清理推文的问题(表情符号,笑脸......)
【发布时间】:2019-03-19 12:57:01
【问题描述】:

我在清理推文时遇到了问题。我有一个将推文保存在 csv 中的过程,然后我对数据进行 pandas 数据框。

x 是来自我的数据框的推文:

'b\'RT @LBC: James O\\\'Brien on Geoffrey Cox\\\'s awaited legal advice: "We are waiting for a single unelected expert to tell us whether or not fore\\xe2\\x80\\xa6\''

更多推文: "b'RT @suzannelynch1: Meanwhile in #Washington... Almost two dozen members of #Congress write to #TheresaMay on eve of #StPatricksDay visit wa\\xe2\\x80\\xa6'

b"RT @KMTV_Kent: #KentTonight Poll:\\nKent\'s MPs will be having their say on Theresa May\'s #Brexit deal today. @SirRogerGaleMP said he\'ll back\\xe2\\x80\\xa6"

结果应该是这样的: James O'Brien on Geoffrey Cox's awaited legal advice: "We are waiting for a single unelected expert to tell us whether or not for' (保留主题标签,只删除不删除 utf8 字符)

我想清理这条推文。我尝试使用正则表达式与 re.sub(my_regex), re.compile ...

我尝试过的不同正则表达式:([\U00010000-\U0010ffff],r'@[A-Za-z0-9]+',https?://[A-Za-z0-9./]+)

我也试过这样:

x.encode('ascii','ignore').decode('utf-8')  

由于双反斜杠,它不起作用,当我这样做时起作用:

'to tell us whether or not fore\xe2\x80\xa6'.encode('ascii','ignore').decode('utf-8')

它返回我:

'to tell us whether or not fore'

有人知道怎么清洗吗? 非常感谢 !

【问题讨论】:

  • 能否提供样本数据
  • 我发了一条推文,你还需要更多吗?
  • 在我看来,那些“转义字符”并不是真正的转义字符,因为反斜杠被转义了。这意味着这些只是文字。 'fore\\xe2\\x80\\xa6\'' 只是文字 fore\xe2\x80\xa6'(其中 \xe2 只是一个 反斜杠 后跟 x 后跟 e 后跟 2)。
  • 这里真正的问题似乎是数据已经损坏了。理想的解决方案是从源头上解决问题,而不是事后解决问题。
  • 某处你已经完成了str(data),其中数据是一个字节字符串。只需将字节字符串解码为 UTF-8。修复混乱而不是解决它。

标签: python regex unicode tweets emoticons


【解决方案1】:

看看有没有帮助

a = 'b\'RT @LBC: James O\\\'Brien on Geoffrey Cox\\\'s awaited legal advice:     "We are waiting for a single unelected expert to tell us whether or not fore\\xe2\\x80\\xa6\''

chars = re.findall("""[\s"'#]+\w+""",a)

''.join([c for c in chars if c])

输出

James O'Brien on Geoffrey Cox's awaited legal advice: "We are waiting for a single unelected expert to tell us whether or not for'

【讨论】:

  • 是的,它可以工作,但是它也会删除主题标签......我将提供更多我的推文示例。如果可能的话,我只想删除任何 utf-8 字符
  • 相信我我知道.. :)
猜你喜欢
  • 2012-09-23
  • 2016-01-16
  • 1970-01-01
  • 2011-04-10
  • 2016-05-31
  • 2015-06-11
  • 2019-07-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多