demoji
demoji is a Python library (current version 1.1.0) designed to accurately find, remove, and replace emojis in text strings. It leverages data from the Unicode Consortium's emoji code repository, bundling this data at install time rather than requiring a runtime download. The library's release cadence is infrequent, with major versions introducing significant, often backwards-incompatible, changes.
Common errors
-
AttributeError: module 'demoji' has no attribute 'download_codes'
cause Attempting to call `demoji.download_codes()` after upgrading to version 1.0.0 or later. This function was removed because emoji data is now bundled with the package.fixRemove the call to `demoji.download_codes()`. Emoji data is automatically available when `demoji` is imported in versions 1.0.0+. -
ModuleNotFoundError: No module named 'demoji'
cause The `demoji` package is not installed in the current Python environment, or the environment where it was installed is not active.fixInstall the package using `pip install demoji`. If using virtual environments, ensure the correct environment is activated before running your script.
Warnings
- breaking Version 1.0.0 introduced backwards-incompatible changes, dropping support for Python 2 and Python 3.5. Users on these older Python versions must remain on `demoji` v0.x.
- breaking From version 1.0.0 onwards, `demoji` bundles emoji data at install time, removing the need for runtime downloads. Consequently, `download_codes()`, `parse_unicode_sequence()`, `parse_unicode_range()`, and `stream_unicodeorg_emojifile()` functions have been removed.
- deprecated The attributes `demoji.DIRECTORY` and `demoji.CACHEPATH` were deprecated in version 1.0.0 as they are no longer functionally used by the package. Accessing them will issue a `FutureWarning`.
Install
-
pip install demoji -
pip install demoji[ujson]
Imports
- demoji
import demoji
- findall
demoji.findall(text_string)
- replace
demoji.replace(text_string, repl='')
- replace_with_desc
emoji.demojize(text_string)
demoji.replace_with_desc(text_string, sep=':')
Quickstart
import demoji
tweet = """
#startspreadingthenews yankees win great start by 🎅🏾 going 5strong innings with 5k's🔥 🐂
solo homerun 🌋🌋 with 2 solo homeruns and👹 3run homerun… 🤡 🚣🏼 👨🏽⚖️ with rbi's … 🔥🔥
🇲🇽 and 🇳🇮 to close the game🔥🔥!!!….
WHAT A GAME!!..
"""
# Find all emojis and their descriptions
emojis_found = demoji.findall(tweet)
print("Emojis found:", emojis_found)
# Replace emojis with an empty string (remove them)
clean_text = demoji.replace(tweet, '')
print("Text with emojis removed:", clean_text)
# Replace emojis with their descriptions (e.g., :fire:)
desc_text = demoji.replace_with_desc(tweet)
print("Text with emoji descriptions:", desc_text)