【发布时间】:2016-04-13 09:22:44
【问题描述】:
我在文件UTF32.red.codes 中有一个表情符号代码列表,格式为纯文本。该文件的纯内容是
\U0001F600
\U0001F601
\U0001F602
\U0001F603
\U0001F604
\U0001F605
\U0001F606
\U0001F609
\U0001F60A
\U0001F60B
基于question,我的想法是从文件的内容中创建正则表达式以捕获表情符号。这是我的最小工作示例
import re
with open('UTF32.red.codes','r') as emof:
codes = [emo.strip() for emo in emof]
emojis = re.compile(u"(%s)" % "|".join(codes))
string = u'string to check \U0001F601'
found = emojis.findall(string)
print found
found 始终为空。我哪里错了?我正在使用 python 2.7
【问题讨论】:
-
您的文件中的
string to check在哪里?我想这不应该在string中。此外,命名变量string可能会造成混淆,因此您可能需要避免这样做。 -
这是要捕获的字符串
\U0001F601 -
然后执行
string = u'\U0001F601'。更好的是,使用不同的变量名称,例如search或类似名称。 -
你说得对。缺少此信息。蟒蛇2.7
-
您是否遇到任何错误?我认为如果我们想解决这个问题,我们需要更多信息。
标签: python regex python-2.7 python-unicode