2014-01-19 26 views
1

我在html文件的多個位置有一些字符串「zyzyzy」。發生次數是1100.用分隔文本替換一些字符串

我需要用豎線(|)分隔的文本替換這些文本,這些文本保留在單獨的文本文件中。

每個分隔文本的長度範圍從1到數百個字符。

它如何使用python-script,notepad ++,sublime text或任何方式來完成。 如果我手動完成,我將不得不在每個文件中複製粘貼1100次。

這是一個使用Notepad ++ python-script插件替換內容的python腳本;我應該將哪些參數傳遞給content.replace函數。

content = editor.getText() 
while "zyzyzy" in content: 
    content = content.replace("zyzyzy", ??) 
/*which arguments should I pass here in place of '??' */ 

notepad.new() 
editor.addText(content) 

感謝

+0

我認爲你將不得不編寫一些代碼..這是很好的,因爲否則這個問題將是offtopic。你知道任何python嗎? – Blorgbeard

+0

@Blorgbeard我加了一些python腳本;我只有很少的編程知識。 – swapna

回答

1

我一起砍死一個python腳本,似乎工作。

# load the pipe-delimited replacements into a list 
with open ("c:\\pipes.txt", "r") as myfile: 
    pipes=myfile.read().split('|') 

# keep track of which replacement we are up to 
ix = 0 

def processmatch(line, match): 
    # we want to use these variables which were declared outside the function 
    global ix 
    global pipes 
    # if we still have replacement text to use.. 
    if ix < len(pipes): 
     # replace this match text with the current replacement 
     editor.setTargetStart(editor.positionFromLine(line) + match.start()) 
     editor.setTargetEnd(editor.positionFromLine(line) + match.end()) 
     editor.replaceTarget(pipes[ix]) 
     # use the next replacement next time 
     ix += 1 

# everything the script does will be undone with a single ctrl+Z 
editor.beginUndoAction() 

# call processmatch for every instance of the target text 
editor.pysearch('zyzyzy', processmatch) 

editor.endUndoAction() 
+0

幫了很多忙;乾杯 – swapna

1

使用崇高的多選。如果您沒有綁定,請將其添加到您的密鑰綁定文件中:

{ "keys": ["ctrl+alt+d"], "command": "find_all_under" }, 

複製分隔文本。選擇第一個發生的zyzyzy,點擊ctrl+alt+d,粘貼。完成。

相關問題