gpt4 book ai didi

python - 使用 Python 和 Notepad++ Unicode 格式的文本文件批量换词

转载 作者:行者123 更新时间:2023-12-04 08:04:00 27 4
gpt4 key购买 nike

我面临的问题是 Unicode 文本文件。Notepad++ 插件>python 脚本。
下面的代码完美地工作并替换包含 wordlist.txt 的单词。只有它适用于英语。非 ASCII 无法搜索。我试过With open('C:\Users\Desktop\wordlist.txt') as f: --> with io.open('C:\Users\Desktop\wordlist.txt', encoding='utf-8') as f:但 Notepad++ 不适用于 Unicode 单词文本文件。
现在我需要帮助如何传递 unicode 字符串进行搜索。在下面的代码中。否则请帮助 A.text 中的批量全字替换的 python 代码使用“单词列表查找并替换为 B.Text 文件中的分隔符”的文件。

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

最佳答案

不要使用单词边界\b这会导致 utf8 字符出现问题。改用环视:

import re

with open('D:\\temp\\wordlist.txt') as f:
for l in f:
s = l.split()
editor.rereplace(r'(?<!\S)' + s[0] + r'(?!\S)', '\t' + s[1])
在哪里:
  • (?<!\S)是一个否定的lookbehind,确保在要修改的单词之前没有非空格
  • (?!\S)是一个负前瞻,确保在要修改的单词后没有非空格

  • 使用您的 2 个示例文件,我得到:
        मारुती
    नामशिवाया
    जयश्रीराम
    जयश्रीराम
  • 注意:我在修改后的单词之前添加了表格以用于可编辑性,将其删除以供您的应用程序使用。

  • 截图:
    enter image description here

    关于python - 使用 Python 和 Notepad++ Unicode 格式的文本文件批量换词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66313496/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com