【问题标题】:Remove non alphanumeric but preserve punctuation删除非字母数字但保留标点符号
【发布时间】:2019-07-31 01:43:27
【问题描述】:

我正在调用 Gmail API 以获取电子邮件的标题。一些标题包含非字母数字字符,例如表情符号、“'”符号等(例如:'\u201cEthnographic')。同时我需要保留单词末尾的标点符号:例如Hello!需要保存。我已经看过许多关于如何摆脱非字母数字的代码示例,但无法完成我想要做的事情。任何反馈表示赞赏。

# Call the api and get the emails
M = json.dumps(message)

temp = message['messages'][0]['payload']

num_found = 0
# get the subject of the emails
for header in temp['headers']:
    # print(header['name'])
    if header['name'] == 'Subject':
        subject = header['value']
        break   

# S contains patterns like "\u201cEthnographic ..."
# or "u2b50\ufe0f best of .."
S = json.dumps(subject)

【问题讨论】:

  • 您是否针对您的输入 (json.dumps(subject)) 测试了以下示例?

标签: python-3.x gmail-api


【解决方案1】:

你看过 emoji python 包了吗?

参考:emoji package documentation

import emoji

def emoji_free(input):
  allchars = [str for str in input]
  emoji_list = [c for c in allchars if c in emoji.UNICODE_EMOJI]
  clean_text = ' '.join([str for str in input.split() if not any(i in str for i in emoji_list)])
  return clean_text

emoji_message = 'This is an emoji ? and the code is designed to remove ? emojis from a string.'

# remove the emojis from the message
clean_message = emoji_free(emoji_message)

print (clean_message)
# output
# This is an emoji and the code is designed to remove emojis from a string.

emoji_message = 'You are a bright \u2b50 with a smiling face \u263A'
print (emoji_message)
# output 
# You are a bright ⭐ with a smiling face ☺

clean_message = emoji_free(emoji_message)
print (clean_message)
# output 
# You are a bright with a smiling face

这是删除与表情符号相关的 unicode 字符串的另一种方法。

import re

emoji_pattern = re.compile("["
            u"\U0001F600-\U0001F64F"  # emoticons
            u"\U0001F300-\U0001F5FF"  # symbols & pictographs
            u"\U0001F680-\U0001F6FF"  # transport & map symbols
            u"\U0001F1E0-\U0001F1FF"  # flags (iOS)
            u"\U00002702-\U000027B0"
            u"\U000024C2-\U0001F251"
            u"\U0001f926-\U0001f937"
            u'\U00010000-\U0010ffff'
            u"\u200d"
            u"\u2640-\u2642"
            u"\u2600-\u2B55"
            u"\u23cf"
            u"\u23e9"
            u"\u231a"
            u"\u3030"
            u"\ufe0f"
"]+", flags=re.UNICODE)

# message with star emoji in unicode
emoji_message = 'You are a bright \u2b50'

# print message with star emoji
print(emoji_message)
# output 
# You are a bright ⭐

# print message without star emoji
print(emoji_pattern.sub(r'', emoji_message)) 
# output 
# You are a bright

【讨论】:

  • 两个答案都很好,但我更喜欢第二个,因为您可以添加更多模式以从文本中删除,例如“符号等”。
  • 第二个肯定更灵活,因为它可以修改。祝您其余的编码工作顺利。
  • 我的问题的实际解决方案是绕过 S = json.dumps(subject) 调用并直接运行此命令:emoji_pattern.sub(r'', subject)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-07
  • 2019-10-29
  • 2014-06-20
  • 2019-06-21
  • 1970-01-01
  • 2014-03-22
  • 1970-01-01
相关资源
最近更新 更多