Skip to content

Instantly share code, notes, and snippets.

@peci1
Created December 25, 2017 23:05
Show Gist options
  • Save peci1/b05ee12ba1f37d399b971bee93ab070b to your computer and use it in GitHub Desktop.
Save peci1/b05ee12ba1f37d399b971bee93ab070b to your computer and use it in GitHub Desktop.
Print the characters corresponding to the given byte sequence in all encodings known to the python interpreter.
#!/usr/bin/env python3
import encodings
import pkgutil
bytes_to_decode = b"\xc4\x9b"
codecs = []
for importer, modname, ispkg in pkgutil.iter_modules(encodings.__path__):
try:
mod = __import__('encodings.' + modname, level=0, fromlist=['*'])
try:
getregentry = mod.getregentry
except AttributeError:
# Not a codec module
continue
codecs.append(modname)
except:
pass
for f in codecs:
try:
dec = bytes_to_decode.decode(f)
print(f + ": " + dec)
except:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment