I know a tiny bit of python, so I would prefer the answer in python.
I'm trying to update the spelling of some old English documents by find and replace a list of old spellings with new spellings.Programming question: How do I make a list of words to be replaced with some other words?
My answer is based upon the assumption that there are some documents in a directory that has words that needs to be replaced. Providing you a replaceStringInFile function that takes the replace string, the string to be replaced and the file file path where the file is located.
漏# -*- coding: utf-8 -*-
漏# Python
漏
漏import os,sys
漏
漏mydir= '/Users/t/web'
漏
漏findStr='%26lt;!DOCTYPE HTML PUBLIC ';-//W3C//DTD HTML 3.2 FINAL//EN';%26gt;'
漏repStr='%26lt;!DOCTYPE HTML PUBLIC ';-//W3C//DTD HTML 4.01
Transitional//EN';%26gt;'
漏
漏def replaceStringInFile(findStr,repStr,fileP鈥?br>
漏 ';replaces all findStr by repStr in file filePath';
漏 tempName=filePath+'~~~'
漏 input = open(filePath)
漏 output = open(tempName,'w')
漏
漏 for s in input:
漏 output.write(s.replace(findStr,repStr))
漏 output.close()
漏 input.close()
漏 os.rename(tempName,filePath)
漏 print filePath
漏
漏def myfun(dummy, dirr, filess):
漏 for child in filess:
漏 if '.html' == os.path.splitext(child)[1] and
漏os.path.isfile(dirr+'/'+child):
漏 replaceStringInFile(findStr,repStr,dirr+鈥?br>
漏os.path.walk(mydir, myfun, 3)
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment