通过删除表情符号来清理字符串
cleaning a string by removing emoticons
我正在使用下面的代码来清理字符串。但是,它无法删除像“”这样的表情符号。有办法吗?
import re
import string
s = '''Hi !こんにちは、私の給料は月額10000ドルです。 XO XO
私はあなたの料理が大好きです
私のフライトはAPX1999です。
私はサッカーの試合を見るのが大好きです。
'''
# replace all ascii chars 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
replaced = re.sub(f'[{string.printable}]', '', s)
print(replaced)
输出:
こんにちは、私の給料は月額ドルです。私はあなたの料理が大好きです私のフライトはです。私はサッカーの試合を見るのが大好きです。
预期输出:
こんにちは、私の給料は月額ドルです。私はあなたの料理が大好きです私のフライトはです。私はサッカーの試合を見るのが大好きです。
试试这个:
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)
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)
こんにちは、私の給料は月額ドルです。私はあなたの料理が大好きです私のフライトはです。私はサッカーの試合を見るのが大好きです。
我正在使用下面的代码来清理字符串。但是,它无法删除像“”这样的表情符号。有办法吗?
import re
import string
s = '''Hi !こんにちは、私の給料は月額10000ドルです。 XO XO
私はあなたの料理が大好きです
私のフライトはAPX1999です。
私はサッカーの試合を見るのが大好きです。
'''
# replace all ascii chars 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
replaced = re.sub(f'[{string.printable}]', '', s)
print(replaced)
输出:
こんにちは、私の給料は月額ドルです。私はあなたの料理が大好きです私のフライトはです。私はサッカーの試合を見るのが大好きです。
预期输出:
こんにちは、私の給料は月額ドルです。私はあなたの料理が大好きです私のフライトはです。私はサッカーの試合を見るのが大好きです。
试试这个:
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)
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)
こんにちは、私の給料は月額ドルです。私はあなたの料理が大好きです私のフライトはです。私はサッカーの試合を見るのが大好きです。