Normalize unicode accents to ASCII in Python
This code converts accented Unicode characters to ASCII equivalents using the standard library's unicodedata module.
Python code
15 linesimport unicodedata
def normalize_accents(text: str) -> str:
"""Convert accented unicode characters to ASCII equivalents."""
decomposed = unicodedata.normalize('NFD', text)
ascii_text = ''.join(
char for char in decomposed
if unicodedata.category(char) != 'Mn'
)
return unicodedata.normalize('NFC', ascii_text)
if __name__ == "__main__":
sample = "Café déjà vu — naïve Zürich über Straße"
print(f"Input: {sample}")
print(f"Output: {normalize_accents(sample)}")
Output
Input: Café déjà vu — naïve Zürich über Straße
Output: Cafe deja vu — naive Zurich uber Strasse
How it works
The function first normalizes the input text using NFD (Canonical Decomposition), which separates base characters from combining diacritical marks. Then it filters out all characters with category 'Mn' (non-spacing marks), effectively removing the accents. Finally, it normalizes again with NFC to combine any remaining characters back into composed form, ensuring consistent Unicode representation.
Common mistakes
- Using NFKD instead of NFD, which also strips compatibility characters and might change symbols unexpectedly
- Forgetting to filter out the 'Mn' category properly, leaving leftover combining marks
- Not applying NFC after filtering, which can result in inconsistencies in output
Variations
- Use the popular `unidecode` library for more aggressive transliteration, including converting characters like 'ß' to 'ss'
- Use `str.maketrans` with a static mapping table for small, fixed sets of accented characters
Real-world use cases
- Normalizing user-generated content for search indexing so queries match regardless of accent usage
- Creating slugified URLs from titles that may contain accented characters
- Preprocessing text data for NLP models that expect ASCII-only input
Sponsored
More from Strings & text
- Automatically Detect Weak Passwords from Large Password Lists in Python easy
- Build CSV row from Python list with proper quoting easy
- Build a Secure Password Strength Checker in Python easy
- Convert Natural Language Dates to Datetime in Python medium
- Count Characters, Words, and Lines in Python Text easy
- Extract Data from Strings in Python: Beginner's Guide easy
Keep learning
Related tutorials and quizzes for this topic.