【发布时间】: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