我在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 :(得分: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()
答案 1 :(得分:1)
使用sublime的多项选择。如果您还没有绑定,请将其添加到您的键绑定文件中:
{ "keys": ["ctrl+alt+d"], "command": "find_all_under" },
复制分隔文字。选择首次出现zyzyzy
,点击ctrl+alt+d
,粘贴。完成。