我正在使用下面的代碼來清理字串。但是,它無法洗掉諸如“????”之類的表情符號。有沒有辦法做到這一點?
import re
import string
s = '''Hi !こんにちは、私の給料は月額10000ドルです。 XO XO
私はあなたの料理が大好きです
私のフライトはAPX1999です。????
私はサッカーの試合を見るのが大好きです。
'''
# replace all ascii chars 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&'()* ,-./:;<=>?@[\]^_`{|}~
replaced = re.sub(f'[{string.printable}]', '', s)
print(replaced)
輸出 :
こんにちは、私の給料は月額ドルです。私はあなたの料理が大好きです私のフライトはです。????私はサッカーの試合を見るのが大好きです。
預期輸出:
こんにちは、私の給料は月額ドルです。私はあなたの料理が大好きです私のフライトはです。私はサッカーの試合を見るのが大好きです。
uj5u.com熱心網友回復:
嘗試這個:
import re
import string
s = '''Hi !こんにちは、私の給料は月額10000ドルです。 XO XO
私はあなたの料理が大好きです
私のフライトはAPX1999です。????
私はサッカーの試合を見るのが大好きです。
'''
# replace all ascii chars 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&'()* ,-./:;<=>?@[\]^_`{|}~
replaced = re.sub(f'[{string.printable}]', '', s)
emoji_pattern = re.compile("["
u"\U0001F600-\U0001F64F" # emoticons
u"\U0001F300-\U0001F5FF" # symbols & pictographs
u"\U0001F680-\U0001F6FF" # transport & map symbols
u"\U0001F1E0-\U0001F1FF" # flags (iOS)
"]", flags=re.UNICODE)
replaced = re.sub(emoji_pattern, '', replaced)
print(replaced)
uj5u.com熱心網友回復:
import re
import string
text = '''Hi !こんにちは、私の給料は月額10000ドルです。 XO XO
私はあなたの料理が大好きです
私のフライトはAPX1999です。????
私はサッカーの試合を見るのが大好きです。'''
pattern = re.compile(
"(["
"\U0001F1E0-\U0001F1FF" # flags (iOS)
"\U0001F300-\U0001F5FF" # symbols & pictographs
"\U0001F600-\U0001F64F" # emoticons
"\U0001F680-\U0001F6FF" # transport & map symbols
"\U0001F700-\U0001F77F" # alchemical symbols
"\U0001F780-\U0001F7FF" # Geometric Shapes Extended
"\U0001F800-\U0001F8FF" # Supplemental Arrows-C
"\U0001F900-\U0001F9FF" # Supplemental Symbols and Pictographs
"\U0001FA00-\U0001FA6F" # Chess Symbols
"\U0001FA70-\U0001FAFF" # Symbols and Pictographs Extended-A
"\U00002702-\U000027B0" # Dingbats
"])"
)
text = re.sub(f'[{string.printable}]', '', text)
text = re.sub(pattern, r'', text)
こんにちは、私の給料は月額ドルです。私はあなたの料理が大好きです私のフライトはです。私はサッカーの試合を見るのが大好きです。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/366284.html
上一篇:無法在函式“cv::dnn::ReadProtoFromTextFile”中打開“face_detector\deploy.prototxt”
