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

      <legend id='5v5qz'><style id='5v5qz'><dir id='5v5qz'><q id='5v5qz'></q></dir></style></legend>

      <small id='5v5qz'></small><noframes id='5v5qz'>

      比较 2 个单独的 csv 文件并将差异写入新的 csv 文件 - Python 2.7

      Compare 2 seperate csv files and write difference to a new csv file - Python 2.7(比较 2 个单独的 csv 文件并将差异写入新的 csv 文件 - Python 2.7)
    1. <i id='vLd49'><tr id='vLd49'><dt id='vLd49'><q id='vLd49'><span id='vLd49'><b id='vLd49'><form id='vLd49'><ins id='vLd49'></ins><ul id='vLd49'></ul><sub id='vLd49'></sub></form><legend id='vLd49'></legend><bdo id='vLd49'><pre id='vLd49'><center id='vLd49'></center></pre></bdo></b><th id='vLd49'></th></span></q></dt></tr></i><div id='vLd49'><tfoot id='vLd49'></tfoot><dl id='vLd49'><fieldset id='vLd49'></fieldset></dl></div>

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

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

        • <legend id='vLd49'><style id='vLd49'><dir id='vLd49'><q id='vLd49'></q></dir></style></legend>
          • <bdo id='vLd49'></bdo><ul id='vLd49'></ul>
                本文介绍了比较 2 个单独的 csv 文件并将差异写入新的 csv 文件 - Python 2.7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试比较 python 中的两个 csv 文件并将差异保存到 python 2.7 中的第三个 csv 文件.

                I am trying to compare two csv files in python and save the difference to a third csv file in python 2.7.

                import csv
                
                f1 = open ("olddata/file1.csv")
                oldFile1 = csv.reader(f1)
                oldList1 = []
                for row in oldFile1:
                    oldList1.append(row)
                
                f2 = open ("newdata/file2.csv")
                oldFile2 = csv.reader(f2)
                oldList2 = []
                for row in oldFile2:
                    oldList2.append(row)
                
                f1.close()
                f2.close()
                
                set1 = tuple(oldList1)
                set2 = tuple(oldList2)
                
                print oldList2.difference(oldList1)
                

                我收到错误消息:

                Traceback (most recent call last):
                  File "compare.py", line 21, in <module>
                    print oldList2.difference(oldList1)
                AttributeError: 'list' object has no attribute 'difference'
                

                我是 python 的新手,一般是编码,我还没有完成这段代码(我必须确保将差异存储到变量并将差异写入新的 csv 文件.).我整天都在努力解决这个问题,但我根本做不到.您的帮助将不胜感激.

                I am new to python, and coding in general, and I am not done with this code just yet (I have to make sure to store the differences to a variable and write the difference to a new csv file.). I have been trying to solve this all day and I simply can't. Your help would be greatly appreciated.

                推荐答案

                差异是什么意思?答案为您提供了两种截然不同的可能性.

                What do you mean by difference? The answer to that gives you two distinct possibilities.

                如果所有列都相同时认为某行相同,那么您可以通过以下代码得到答案:

                If a row is considered same when all columns are same, then you can get your answer via the following code:

                import csv
                
                f1 = open ("olddata/file1.csv")
                oldFile1 = csv.reader(f1)
                oldList1 = []
                for row in oldFile1:
                    oldList1.append(row)
                
                f2 = open ("newdata/file2.csv")
                oldFile2 = csv.reader(f2)
                oldList2 = []
                for row in oldFile2:
                    oldList2.append(row)
                
                f1.close()
                f2.close()
                
                print [row for row in oldList1 if row not in oldList2]
                

                但是,如果两行相同且某个关键字段(即列)相同,那么以下代码将为您提供答案:

                However, if two rows are same if a certain key field (i.e. column) is same, then the following code will give you your answer:

                import csv
                
                f1 = open ("olddata/file1.csv")
                oldFile1 = csv.reader(f1)
                oldList1 = []
                for row in oldFile1:
                    oldList1.append(row)
                
                f2 = open ("newdata/file2.csv")
                oldFile2 = csv.reader(f2)
                oldList2 = []
                for row in oldFile2:
                    oldList2.append(row)
                
                f1.close()
                f2.close()
                
                keyfield = 0 # Change this for choosing the column number
                
                oldList2keys = [row[keyfield] for row in oldList2]
                print [row for row in oldList1 if row[keyfield] not in oldList2keys]
                

                注意: 对于超大文件,上述代码可能运行缓慢.相反,如果您希望通过散列加速代码,您可以在使用以下代码转换 oldList 后使用 set:

                Note: The above code might run slow for extremely large files. If instead, you wish to speed up code through hashing, you can use set after converting the oldLists using the following code:

                set1 = set(tuple(row) for row in oldList1)
                set2 = set(tuple(row) for row in oldList2)
                

                在这之后,你可以使用set1.difference(set2)

                这篇关于比较 2 个单独的 csv 文件并将差异写入新的 csv 文件 - Python 2.7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                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中的另一个文件中查找一个文本文件的条目)
                  <tbody id='1mk92'></tbody>
                  <bdo id='1mk92'></bdo><ul id='1mk92'></ul>

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

                        <small id='1mk92'></small><noframes id='1mk92'>

                      • <tfoot id='1mk92'></tfoot>