2017-02-21 5 views
0

つぶやきから👉👌💦✨などの特殊文字を削除する必要があります。そのために、私はこの戦略(Iは、Python 3を使用)に続く:つぶやきから特殊文字( ``ŒðŸ'`など)を削除する方法

  1. はそうÃ\xc3\になり、六角などの特殊文字を取得するために、文字列にバイトからつぶやきを変換します。
  2. 正規表現を使用して、b'b"(文字列の先頭)と'または"(文字列の最後)を削除します。
  3. 最後に、正規表現を使用して16進表現を削除します。

    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データの特殊文字を整理するためのより良い、より直接的な方法があるのだろうかと思っていました。どんな助けもありがとう。

答えて

1

私はあなたがASCII文字のみを探している場合、これは、正常に動作すると思います:

initial_str = 'Some text 👉👌💦✨ and some more text' 
clean_str = ''.join([c for c in initial_str if ord(c) < 128]) 
print(clean_str) # Some text and some more text 

あなたはord(c) in range()を行い、そしてそれをあなたが(絵文字を含める場合もある)維持したいテキストの範囲を与えることができます。

関連する問題