Skip to content

Instantly share code, notes, and snippets.

@fnurl
Created August 10, 2012 17:54
Show Gist options
  • Save fnurl/3316165 to your computer and use it in GitHub Desktop.
Save fnurl/3316165 to your computer and use it in GitHub Desktop.
Read file with tab separated search and replace entries (one per line) and apply them to a target file and output to std-out.
#!/usr/bin/python
# -*- coding: utf_8 -*-
#
import sys
import re
import os
import codecs
targetfile = sys.argv[1]
replacements = sys.argv[2]
# read escaped chars into dictionary, escape sequence is key
charDic = {}
replaceFile = codecs.open(replacements, 'r', encoding='utf8')
for line in replaceFile:
lineList = line.strip().split('\t')
charDic[lineList[0]] = lineList[1]
replaceFile.close()
for line in codecs.open(targetfile, 'r', encoding='utf8'):
# replace escaped chars with our stuff
for key in charDic.keys():
line = line.replace(key, charDic[key])
sys.stdout.write(line.encode('utf8'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment