これは私が絵文字情報をロードするために作成することになったコードです。 get_emoji
関数はデータファイルを取得し、解析し、列挙コールバックを呼び出します。残りのコードは、これを使用して必要な情報のJSONファイルを生成します。
#!/usr/bin/env python3
# Generates a list of emoji characters and names in JS format
import urllib.request
import unicodedata
import re, json
'''
Enumerates the Emoji characters that match an attributes from the Unicode standard (the Emoji list).
@param on_emoji A callback that is called with each found character. Signature `on_emoji(code_point_value)`
@param attribute The attribute that is desired, such as `Emoji` or `Emoji_Presentation`
'''
def get_emoji(on_emoji, attribute):
with urllib.request.urlopen('http://www.unicode.org/Public/emoji/5.0/emoji-data.txt') as f:
content = f.read().decode(f.headers.get_content_charset())
cldr = re.compile('^([0-9A-F]+)(..([0-9A-F]+))?([^;]*);([^#]*)#(.*)$')
for line in content.splitlines():
m = cldr.match(line)
if m == None:
continue
line_attribute = m.group(5).strip()
if line_attribute != attribute:
continue
code_point = int(m.group(1),16)
if m.group(3) == None:
on_emoji(code_point)
else:
to_code_point = int(m.group(3),16)
for i in range(code_point,to_code_point+1):
on_emoji(i)
# Dumps the values into a JSON format
def print_emoji(value):
c = chr(value)
try:
obj = {
'code': value,
'name': unicodedata.name(c).lower(),
}
print(json.dumps(obj),',')
except:
# Unicode DB is likely outdated in installed Python
pass
print("module.exports = [")
get_emoji(print_emoji, "Emoji_Presentation")
print("]")
それが私の元の問題を解決しました。質問そのものに答えるためには、結果を辞書に貼り付けて検索するだけです。
[emojisをPythonの文字列から削除する]可能な複製(https://stackoverflow.com/questions/33404752/removing-emojis-from-a-string-in-python) – kabanus
@kabanus重複していません。他の質問は、文字の無作為なリストを絵文字として指定します。私は、特にUnicode標準によって絵文字としてマークされているものについて尋ねています。 –