Created
January 23, 2024 10:57
-
-
Save jgs03177/aef6eef884b203c7a10b345c62812253 to your computer and use it in GitHub Desktop.
fix dict unicode escape (maybe because of json)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _decode_unicode_escape_in_dict_(target_dict): | |
new_dict = {} | |
for key1, value1 in target_dict.items(): | |
key1 = key1.encode().decode("unicode-escape") | |
if isinstance(value1, dict): | |
value1 = _decode_unicode_escape_in_dict_(value1) | |
elif isinstance(value1, str): | |
value1 = value1.encode().decode("unicode-escape") | |
new_dict[key1] = value1 | |
return new_dict | |
if __name__ == '__main__': | |
dict1 = {"aaa": "\\uad50\\ud5a5\\uace1", "\\uad50\\ud5a5\\uace1": dict({"ccc": "\\uad50\\ud5a5\\uace1"})} | |
dict1 = _decode_unicode_escape_in_dict_(dict1) | |
print(dict1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment