【问题标题】:Python Script for search and replace with "match words only" option用于搜索和替换“仅匹配单词”选项的 Python 脚本
【发布时间】:2013-07-17 21:35:19
【问题描述】:

Py 文件:命名为葡萄牙语翻译器.py

with open('C:/Users/User/Desktop/Portuguesetranslator.txt') as f:
    for l in f:
        s = l.split('*')
        editor.replace(s[0],s[1])

我在 Notepad++ 内的 Python 脚本中创建了它

然后我有我的“数据库”,叫做葡萄牙语翻译器.txt

又分为

结果*结果* 事件*事件* .... 以及更多这样的 1k++ 示例

然后我做的过程是.. 我打开第三个标签...从互联网复制文本..并放入该标签..然后我通过按插件/python脚本/葡萄牙语翻译器运行脚本

它在我的整个文档中运行并搜索和替换..

那我做错了什么?

【问题讨论】:

  • 你能发布你的文本文件吗?这将有助于了解更多。
  • Helton Por 喜欢我 ajuda cara! o 巴西的精确语音! :(
  • @RenanCidale no meu perfil tem correio eletrônico, só não garanto ajuda rápida!

标签: python replace


【解决方案1】:

试试正则表达式。 \b 是正则表达式中的单词边界命令。这意味着在正则表达式中的那个点,你必须在一个词的边界上(而不是在一个词的中间)。你可以把它包裹在你的s[0]

import re
with open('C:/Users/User/Desktop/Portuguesetranslator.txt') as f:
    for l in f:
        s = l.split('*')
        editor = re.sub(r'\b' + s[0] + r'\b', s[1], editor)

编辑 - 对于记事本++,看起来您希望最后一行是这样的:

editor.pyreplace(r'\b' + s[0] + r'\b', s[1])

【讨论】:

  • 所以我只需要复制该脚本并替换为我的吗?.. 对于我继续使用相同模型编写的数据库?结果结果 ?
  • 所以..我替换了..并尝试运行脚本..但它没有翻译任何东西..
  • 在文本文件中..单词必须如何放置??结果resultado ?
  • import re with open('C:/Users/User/Desktop/Portuguesetranslator.txt') as f: for l in f: s = l.split('*') re.sub( r'\b' + s[0] + r'\b', s[1], editor) 在哪里是它在脚本文件中的所有内容
  • 是的..它是我的整个脚本..我在我的文本文件中运行该脚本..该脚本基于在 C:/Users/User/Desktop/Portuguesetranslator.txt 中建立的数据库中可以在脚本中看到
【解决方案2】:

@JoshG79 的回答对我来说不太适用(N++ 7.5.1),只需要修改最后一行:

with open('C:\Users\Administrator\Desktop\IPL\WIP\EnergyTagSubstitutions.txt') as f: for l in f: s = l.split() editor.rereplace(r'\b' + s[0] + r'\b', s[1])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-16
    • 1970-01-01
    • 1970-01-01
    • 2013-07-17
    • 1970-01-01
    • 1970-01-01
    • 2011-02-17
    • 1970-01-01
    相关资源
    最近更新 更多