用分隔的tex替换一些字符串

2024-04-23 07:25:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些字符串“zyzy”在一个html文件的多个地方。发生的次数是1100次。你知道吗

我需要用竖线(|)分隔的文本替换这些文本,这些文本保存在单独的文本文件中。你知道吗

每个分隔文本的长度从一个字符到数百个字符不等。你知道吗

如何使用python脚本、notepad++、sublime文本或其他任何方式来完成。 如果我这样做手动我将不得不复制粘贴1100次在每个文件。你知道吗

这是一个用Notepad++python脚本插件替换内容的python脚本;我应该传递哪些参数内容.替换功能。你知道吗

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)

谢谢


Tags: 文件字符串in文本脚本内容html地方
2条回答

使用sublime的多种选择。如果您还没有绑定,请将其添加到keybinding文件:

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

复制分隔文本。选择第一次出现的zyzyzy,点击ctrl+alt+d,粘贴。完成。你知道吗

我拼凑了一个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()

相关问题 更多 >