如何解决使用 Javascript 将 Notepad++ 中的参考文件中的 [[Words]] 替换为其他 [[Words]]
Apple=Apfel
Apple pie=Apfelkuchen
Banana=Banane
Bananaisland=Bananen Insel
Cherry=Kirsche
Train=Zug
...500+ 多行这样的
现在我有一个文件需要处理文本。只需要替换文本的某些部分,例如:
The [[Apple]] was next to the [[Banana]]. Meanwhile the [[Cherry]] was chilling by the [[Train]].
The [[Apple pie]] tastes great on the [[Bananaisland]].
结果需要
The [[Apfel]] was next to the [[Banane]]. Meanwhile the [[Kirsche]] was chilling by the [[Zug]].
The [[Apfelkuchen]] tastes great on the [[Bananen Insel]].
手动复制/粘贴的事件太多了。如上所述,搜索 [[XXX]] 并从另一个文件替换的简单方法是什么?
我尝试为此寻求帮助很多小时,但无济于事。我得到的最接近的是这个脚本:
import re
separators = "=","\n"
def custom_split(sepr_list,str_to_split):
# create regular expression dynamically
regular_exp = '|'.join(map(re.escape,sepr_list))
return re.split(regular_exp,str_to_split)
with open('D:/_working/paired-search-replace.txt') as f:
for l in f:
s = custom_split(separators,l)
editor.replace(s[0],s[1])
不过这样会替换的太多,或者不一致。例如。 [[Apple]] 被 [[Apfel]] 正确替换,但 [[File:Apple.png]] 被 [[File:Apfel.png]] 错误替换,[[Apple pie]] 被 [[Apfel] 替换pie]],所以我尝试连续几个小时调整正则表达式,但无济于事。有没有人有任何信息 - 请用非常简单的术语 - 我如何解决这个问题/实现我的目标?
解决方法
这有点棘手,因为 [ 是正则表达式中的元字符。
我确信有一种更有效的方法可以做到这一点,但这很有效:
replaces="""Apple=Apfel
Apple pie=Apfelkuchen
Banana=Banane
Bananaisland=Bananen Insel
Cherry=Kirsche
Train=Zug"""
text = """
The [[Apple]] was next to the [[Banana]]. Meanwhile the [[Cherry]] was chilling by the [[Train]].
The [[Apple pie]] tastes great on the [[Bananaisland]].
"""
if __name__ == '__main__':
import re
for replace in replaces.split('\n'):
english,german = replace.split('=')
text = re.sub(rf'\[\[{english}\]\]',f'[[{german}]]',text)
print(text)
输出:
The [[Apfel]] was next to the [[Banane]]. Meanwhile the [[Kirsche]] was chilling by the [[Zug]].
The [[Apfelkuchen]] tastes great on the [[Bananen Insel]].
,
首先,读入带有翻译的文件:
translations={}
with open('file/with/translations.txt','r',encoding='utf-8') as f:
for line in f:
items = line.strip().split('=',1)
translations[items[0]] = items[1]
我假设短语/单词在文件中是唯一的。
然后,您需要匹配 [[
和 ]]
之间的所有子字符串,捕获其间的文本(使用像 \[\[(.*?)]]
这样的正则表达式,参见 the online demo),检查是否有键与translations
字典中的组1值,如果有这样的键,则替换为[[
+字典值+]]
,如果没有这样的键,则返回整个匹配项翻译:
text = """The [[Apple]] was next to the [[Banana]]. Meanwhile the [[Cherry]] was chilling by the [[Train]].
The [[Apple pie]] tastes great on the [[Bananaisland]]."""
import re
translated_text = re.sub(r"\[\[(.*?)]]",lambda x: f'[[{translations[x.group(1)]}]]' if x.group(1) in translations else x.group(),text)
输出:
>>> translated_text
'The [[Apfel]] was next to the [[Banane]]. Meanwhile the [[Kirsche]] was chilling by the [[Zug]]. \nThe [[Apfelkuchen]] tastes great on the [[Bananen Insel]].'
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。