• <small id='BBZ3h'></small><noframes id='BBZ3h'>

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

      2. 比较两个 CSV 文件并搜索相似项目

        Compare two CSV files and search for similar items(比较两个 CSV 文件并搜索相似项目)

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

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

                • <legend id='uPBu7'><style id='uPBu7'><dir id='uPBu7'><q id='uPBu7'></q></dir></style></legend>
                    <tbody id='uPBu7'></tbody>
                • 本文介绍了比较两个 CSV 文件并搜索相似项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  所以我有两个 CSV 文件,我试图比较它们并获得相似项目的结果.第一个文件 hosts.csv 如下所示:

                  So I've got two CSV files that I'm trying to compare and get the results of the similar items. The first file, hosts.csv is shown below:

                  Path    Filename    Size    Signature
                  C:     a.txt       14kb    012345
                  D:     b.txt       99kb    678910
                  C:     c.txt       44kb    111213
                  

                  第二个文件masterlist.csv如下所示:

                  The second file, masterlist.csv is shown below:

                  Filename    Signature
                  b.txt       678910
                  x.txt       111213
                  b.txt       777777
                  c.txt       999999
                  

                  如您所见,行不匹配,masterlist.csv 始终大于 hosts.csv 文件.我想搜索的唯一部分是签名部分.我知道这看起来像:

                  As you can see the rows do not match up and the masterlist.csv is always larger than the hosts.csv file. The only portion that I'd like to search for is the Signature portion. I know this would look something like:

                  hosts[3] == masterlist[1]
                  

                  我正在寻找一种解决方案,它将为我提供如下内容(基本上是带有新 RESULTS 列的 hosts.csv 文件):

                  I am looking for a solution that will give me something like the following (basically the hosts.csv file with a new RESULTS column):

                  Path    Filename    Size    Signature    RESULTS
                  C:     a.txt       14kb    012345       NOT FOUND in masterlist
                  D:     b.txt       99kb    678910       FOUND in masterlist (row 1)
                  C:     c.txt       44kb    111213       FOUND in masterlist (row 2)
                  

                  我搜索了这些帖子,发现了与此类似的内容 这里 但我不太明白,因为我还在学习python.

                  I've searched the posts and found something similar to this here but I don't quite understand it as I'm still learning python.

                  编辑使用 Python 2.6

                  Edit Using Python 2.6

                  推荐答案

                  虽然我的解决方案工作正常,但请查看下面 Martijn 的答案以获得更有效的解决方案.

                  While my solution works correctly, check out Martijn's answer below for a more efficient solution.

                  您可以在这里找到python CSV模块的文档.

                  You can find the documentation for the python CSV module here.

                  你正在寻找的是这样的:

                  What you're looking for is something like this:

                  import csv
                  
                  f1 = file('hosts.csv', 'r')
                  f2 = file('masterlist.csv', 'r')
                  f3 = file('results.csv', 'w')
                  
                  c1 = csv.reader(f1)
                  c2 = csv.reader(f2)
                  c3 = csv.writer(f3)
                  
                  masterlist = list(c2)
                  
                  for hosts_row in c1:
                      row = 1
                      found = False
                      for master_row in masterlist:
                          results_row = hosts_row
                          if hosts_row[3] == master_row[1]:
                              results_row.append('FOUND in master list (row ' + str(row) + ')')
                              found = True
                              break
                          row = row + 1
                      if not found:
                          results_row.append('NOT FOUND in master list')
                      c3.writerow(results_row)
                  
                  f1.close()
                  f2.close()
                  f3.close()
                  

                  这篇关于比较两个 CSV 文件并搜索相似项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What happens when you compare 2 pandas Series(当你比较 2 个 pandas 系列时会发生什么)
                  Quickly find differences between two large text files(快速查找两个大文本文件之间的差异)
                  Python - Compare 2 files and output differences(Python - 比较 2 个文件和输出差异)
                  Why do comparisions between very large float values fail in python?(为什么在 python 中非常大的浮点值之间的比较会失败?)
                  Dictionary merge by updating but not overwriting if value exists(字典通过更新合并,但如果值存在则不覆盖)
                  Find entries of one text file in another file in python(在python中的另一个文件中查找一个文本文件的条目)
                  <legend id='LDrcl'><style id='LDrcl'><dir id='LDrcl'><q id='LDrcl'></q></dir></style></legend>

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

                      <tfoot id='LDrcl'></tfoot>

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

                            <tbody id='LDrcl'></tbody>