Skip to content

Instantly share code, notes, and snippets.

@xexu
Created April 28, 2013 21:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xexu/5478411 to your computer and use it in GitHub Desktop.
Save xexu/5478411 to your computer and use it in GitHub Desktop.
replace utility for large files
# -*- coding: utf-8 -*-
import os, codecs, sys
dir = os.getcwd()
file = sys.argv[1]
Dict = [['a', 'b'], ['c', 'd']]
fname = os.path.join(dir, file)
inFile = codecs.open(fname, "r", "utf-8")
outFile = codecs.open(fname + ".new", "w", "utf-8")
for line in inFile:
for Dictrow in Dict:
line = line.replace(Dictrow[0], Dictrow[1])
outFile.write(line)
print line
inFile.close()
outFile.close()
os.rename(fname + ".new", fname)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment