Skip to content

Instantly share code, notes, and snippets.

@aparrish
Created April 25, 2011 12:20
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save aparrish/940436 to your computer and use it in GitHub Desktop.
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