有目錄多個文件擴展名爲.txt,.dox,.qcr等搜尋替換從.txt文件數字符串中的蟒蛇
我需要列出txt文件,搜索&替換文本僅來自每個txt文件。
需要搜索$$ \ d ...其中\ d代表數字1,2,3 ..... 100。需要用xxx替換 。
請讓我知道這個python腳本。
在此先感謝。
-Shrinivas
#created following script, it works for single txt files, but it is not working for txt files more than one lies in directory
。 -----
def replaceAll(file,searchExp,replaceExp):
for line in fileinput.input(file, inplace=1):
if searchExp in line:
line = line.replace(searchExp,replaceExp)
sys.stdout.write(line)
#following code is not working, i expect to list out the files start #with "um_*.txt", open the file & replace the "$$\d" with replaceAll function.
for um_file in glob.glob('*.txt'):
t = open(um_file, 'r')
replaceAll("t.read","$$\d","xxx")
t.close()
你好,謝謝你,這是不工作的任何數字「\ d」 ......我的意思是searchExp「$$ \ d」沒有得到換成「 xxx「,同樣的代碼工作時,我使用數字說1,然後replaceExp將$$ 1。 「\ d」並非全球取代數字。 – Shrinivas
@Shrinivas看看吧。 –