Created
August 10, 2012 17:54
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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