Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
replace strings in file with other random strings having the same number of letters
# usage:
# python replacer.py wordlist input_file output_file
import sys
import re
import random
words = dict()
allwords = set()
for line in open(sys.argv[1]):
line = line.strip()
if len(line) not in words:
words[len(line)] = []
words[len(line)].append(line)
allwords.add(line)
def match_case(instr, fromstr):
assert(len(instr) == len(fromstr))
return "".join([instr[i].upper() if fromstr[i].isupper() \
else instr[i].lower() for i in range(len(instr))])
def wordrepl(mobj):
match = mobj.group(0)
if match.lower() in allwords:
return match_case(random.choice(words[len(mobj.group(0))]), match)
else:
return mobj.group(0)
contents = open(sys.argv[2], "rb").read()
contents = re.sub(r"([a-zA-Z]{3,})", wordrepl, contents)
outputf = open(sys.argv[3], "wb")
outputf.write(contents)
outputf.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment