2014-01-19 24 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,粘贴。完成。

相关问题