Skip to content

Instantly share code, notes, and snippets.

@ischurov
Created December 25, 2016 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ischurov/cf4c0eae198ea1d63ad4b8b4db72558d to your computer and use it in GitHub Desktop.
Save ischurov/cf4c0eae198ea1d63ad4b8b4db72558d to your computer and use it in GitHub Desktop.
A helper function useful to decode cp866-encoded filenames in tar archives.
def decode_escaped_cp866(s):
out = []
for token in re.finditer(r"%([0-9A-F]{2})|(.)", s):
if token.group(1) is not None:
out.append(bytes([int(token.group(1), 16)]))
elif token.group(2) is not None:
out.append(token.group(2).encode('utf-8'))
return b"".join(out).decode('cp866')
print(decode_escaped_cp866("%8C%A0%E0%A8%EF - %84%87"))
# Мария - ДЗ
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment