Skip to content

Instantly share code, notes, and snippets.

@rj76
Created July 15, 2021 08:01
Show Gist options
  • Save rj76/e767f504cde92f92ae550e55018a0045 to your computer and use it in GitHub Desktop.
Save rj76/e767f504cde92f92ae550e55018a0045 to your computer and use it in GitHub Desktop.
Find encodings for a char
import pkgutil
import encodings
import os
def all_encodings():
modnames = set([modname for importer, modname, ispkg in pkgutil.walk_packages(
path=[os.path.dirname(encodings.__file__)], prefix='')])
aliases = set(encodings.aliases.aliases.values())
return modnames.union(aliases)
text = b'\xe1'
for enc in all_encodings():
try:
msg = text.decode(enc)
except Exception:
continue
if msg == 'á':
print('Decoding {t} with {enc} is {m}'.format(t=text, enc=enc, m=msg))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment