Python Convert complex dictionary of strings from Unicode to ASCII
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Python is renowned for its versatility and ease of use, and it provides powerful tools to work with various data structures such as dictionaries. In certain scenarios, you might encounter complex dictionaries with string data in Unicode format that need conversion to ASCII. This task can be nuanced, especially with dictionaries deeply nested or having Unicode characters that require special handling.
Understanding Unicode and ASCII
Unicode is a computing standard that encompasses a vast array of characters from multiple languages and scripts to ensure global text representation. It includes characters from the Latin alphabet, Cyrillic, CJK (Chinese, Japanese, Korean), and many other scripts.
ASCII (American Standard Code for Information Interchange), on the other hand, represents English characters using a 7-bit binary number. It includes codes for 128 characters consisting of:
- The digits
0-9 - Lower and uppercase English letters
a-z,A-Z - Basic punctuation and control characters
Unicode extends ASCII with additional characters, requiring the conversion of some Unicode characters into their closest ASCII equivalents for compatibility in certain systems.
Problem Statement
When working with complex dictionaries in Python, especially those fetched from APIs or user inputs in international applications, you may need to convert the strings from Unicode to ASCII. This conversion can be particularly challenging with a deeply nested dictionary, requiring a recursive approach.
Implementation
Example of a Complex Dictionary
A typical complex dictionary with Unicode strings might look like this:
- The **
unicode_to_ascii** function is recursive. It traverses the dictionary, list, or any other data structure. - It checks if an item is a dictionary, then calls itself on each key-value pair.
- If a list is encountered, it iteratively calls itself on each element.
- If a string is found, it normalizes the Unicode string to a canonical composition (
NFKD), replaces Unicode characters with ASCII approximations, and ignores characters that cannot be converted. - Non-string values are returned without alteration.
- Diacritics: Characters like
éandöare converted toeando, respectively. - Non-Conforming: Characters that do not map neatly (e.g., certain symbols or scripts outside the ASCII range) will be omitted.
'ignore': Omits characters that can't be encoded.'replace': Replaces unencodable characters with a replacement character.'backslashreplace': Uses backslash escape sequences.
Related reading
- Python creating a dictionary of lists
- Python csv string to array
- Python data structure sort list alphabetically
- Python dataclass from a nested dict
- Python Convert timedelta to int in a dataframe
- Python Create unix timestamp five minutes in the future
- Python dictionary are keys and values always the same order?
- Python Dictionary Comprehension

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.