Skip to content

Instantly share code, notes, and snippets.

@infval
Last active February 3, 2020 18:05
Show Gist options
  • Save infval/60c1bdd2c69f38cc81d2b175133dd07d to your computer and use it in GitHub Desktop.
Save infval/60c1bdd2c69f38cc81d2b175133dd07d to your computer and use it in GitHub Desktop.
Translate characters
#!/usr/bin/env python3
import argparse
from pathlib import Path
parser = argparse.ArgumentParser(description='Translate characters')
parser.add_argument('input', help='input file')
parser.add_argument('table', help='table file, "A=Z\\nB=2\\nK=M"')
parser.add_argument('output', help='output file')
parser.add_argument('-e', '--encoding', default="utf-8",
help='default utf-8, see https://docs.python.org/3/library/codecs.html#standard-encodings')
args = parser.parse_args()
pinp = Path(args.input)
ptab = Path(args.table)
pout = Path(args.output)
table = ptab.read_text(encoding=args.encoding)
table = {ord(line[0]):ord(line[-1]) for line in table.split("\n") if len(line) >= 3}
txt = pinp.read_text(encoding=args.encoding)
new_txt = txt.translate(table)
pout.write_text(new_txt, encoding=args.encoding)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment