1. <i id='oZN0j'><tr id='oZN0j'><dt id='oZN0j'><q id='oZN0j'><span id='oZN0j'><b id='oZN0j'><form id='oZN0j'><ins id='oZN0j'></ins><ul id='oZN0j'></ul><sub id='oZN0j'></sub></form><legend id='oZN0j'></legend><bdo id='oZN0j'><pre id='oZN0j'><center id='oZN0j'></center></pre></bdo></b><th id='oZN0j'></th></span></q></dt></tr></i><div id='oZN0j'><tfoot id='oZN0j'></tfoot><dl id='oZN0j'><fieldset id='oZN0j'></fieldset></dl></div>
      <bdo id='oZN0j'></bdo><ul id='oZN0j'></ul>

  2. <small id='oZN0j'></small><noframes id='oZN0j'>

  3. <legend id='oZN0j'><style id='oZN0j'><dir id='oZN0j'><q id='oZN0j'></q></dir></style></legend>
    <tfoot id='oZN0j'></tfoot>

      在文件python中查找和替换多个单词

      时间:2023-08-30

          <tbody id='wZQWI'></tbody>
            <i id='wZQWI'><tr id='wZQWI'><dt id='wZQWI'><q id='wZQWI'><span id='wZQWI'><b id='wZQWI'><form id='wZQWI'><ins id='wZQWI'></ins><ul id='wZQWI'></ul><sub id='wZQWI'></sub></form><legend id='wZQWI'></legend><bdo id='wZQWI'><pre id='wZQWI'><center id='wZQWI'></center></pre></bdo></b><th id='wZQWI'></th></span></q></dt></tr></i><div id='wZQWI'><tfoot id='wZQWI'></tfoot><dl id='wZQWI'><fieldset id='wZQWI'></fieldset></dl></div>

              <bdo id='wZQWI'></bdo><ul id='wZQWI'></ul>

              <small id='wZQWI'></small><noframes id='wZQWI'>

                <legend id='wZQWI'><style id='wZQWI'><dir id='wZQWI'><q id='wZQWI'></q></dir></style></legend>

                <tfoot id='wZQWI'></tfoot>
              • 本文介绍了在文件python中查找和替换多个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我从 这里.

                f1 = open('file1.txt', 'r')
                f2 = open('file2.txt', 'w')
                for line in f1:
                    f2.write(line.replace('old_text', 'new_text'))
                f1.close()
                f2.close()
                

                但我不知道如何用各自的新词替换多个词.在这个例子中,如果我想找到一些像 (old_text1,old_text2,old_text3,old_text4) 这样的词并用它各自的新词替换 (new_text1,new_text2,new_text3,new_text4).

                But I am not able to figure out how to replace multiple words with respective new words. In this example if I want to find some words like (old_text1,old_text2,old_text3,old_text4) and replace by its respective new words (new_text1,new_text2,new_text3,new_text4).

                提前致谢!

                推荐答案

                你可以遍历你的检查词并使用 zip 替换词然后替换.

                You can iterate over your check words and toReplace words using zip and then replace.

                例如:

                checkWords = ("old_text1","old_text2","old_text3","old_text4")
                repWords = ("new_text1","new_text2","new_text3","new_text4")
                
                for line in f1:
                    for check, rep in zip(checkWords, repWords):
                        line = line.replace(check, rep)
                    f2.write(line)
                f1.close()
                f2.close()
                

                这篇关于在文件python中查找和替换多个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:在 Python 中实现不相交集系统 下一篇:Python中的序列查找函数

                相关文章

                  <bdo id='8eobE'></bdo><ul id='8eobE'></ul>

                <tfoot id='8eobE'></tfoot>

                1. <small id='8eobE'></small><noframes id='8eobE'>

                    <i id='8eobE'><tr id='8eobE'><dt id='8eobE'><q id='8eobE'><span id='8eobE'><b id='8eobE'><form id='8eobE'><ins id='8eobE'></ins><ul id='8eobE'></ul><sub id='8eobE'></sub></form><legend id='8eobE'></legend><bdo id='8eobE'><pre id='8eobE'><center id='8eobE'></center></pre></bdo></b><th id='8eobE'></th></span></q></dt></tr></i><div id='8eobE'><tfoot id='8eobE'></tfoot><dl id='8eobE'><fieldset id='8eobE'></fieldset></dl></div>
                  1. <legend id='8eobE'><style id='8eobE'><dir id='8eobE'><q id='8eobE'></q></dir></style></legend>