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

    <bdo id='NpOrm'></bdo><ul id='NpOrm'></ul>
    <tfoot id='NpOrm'></tfoot>

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

      1. Python - 比较 2 个文件和输出差异

        时间:2023-11-08

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

          <tfoot id='iEJwe'></tfoot>
              <tbody id='iEJwe'></tbody>

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

                • 本文介绍了Python - 比较 2 个文件和输出差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的目标是编写一个脚本来比较文件中的每一行,并根据这个比较,创建一个新文件,其中包含第二个文件中没有的文本行.

                  I'm aiming to write a script that will compare each line within a file, and based upon this comparison, create a new file containing the lines of text which aren't in the second file.

                  例如;

                  **File 1:** 
                  
                  Bob:20 
                  Dan:50 
                  Brad:34 
                  Emma:32 
                  Anne:43
                  
                  **File 2:**
                  
                  Dan:50
                  Emma:32
                  Anne:43
                  

                  新输出(文件 3):

                  Bob:20
                  Brad:34
                  

                  我知道这需要如何完成,但不完全是:

                  I have some idea of how this needs to be done, but not exactly:

                  def compare(File1,File2):
                     with open(File1, "a") as f1:
                         lines = f1.readlines()
                         string = line.split(':')
                     with open(File2, "a") as f2:
                         lines = f2.readlines()
                         string2 = line.split(':')
                         if string[0] == string[1]:
                             with open("newfile2.txt", "w") as f3:
                              ....
                  

                  我想我需要一些类似的东西,然后比较每个文件每一行的字符串[0],但从这一点上我真的一无所知.

                  I think I need something along the lines of this and then to compare the string[0] from each line of each file but I'm really clueless from this point.

                  我们非常欢迎任何帮助.

                  Any help would be extremely welcomed.

                  推荐答案

                  这对我有用:

                  def compare(File1,File2):
                      with open(File1,'r') as f:
                          d=set(f.readlines())
                  
                  
                      with open(File2,'r') as f:
                          e=set(f.readlines())
                  
                      open('file3.txt','w').close() #Create the file
                  
                      with open('file3.txt','a') as f:
                          for line in list(d-e):
                             f.write(line)
                  

                  您需要比较 readlines 集并找出 file2 中不存在的行.然后,您可以将这些行附加到新文件中.

                  You need to compare the readlines set and find out lines that are not present in file2. You can then append these lines to the new file.

                  这篇关于Python - 比较 2 个文件和输出差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为什么在 python 中非常大的浮点值之间的比较会失败? 下一篇:快速查找两个大文本文件之间的差异

                  相关文章

                    1. <small id='Cahje'></small><noframes id='Cahje'>

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

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

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