【问题标题】:Comparing 2 huge (5-6 GB) csv files and count the number of matching and unmatched no. of rows比较 2 个巨大的 (5-6 GB) csv 文件并计算匹配和不匹配的数量。行数
【发布时间】:2019-05-20 07:23:29
【问题描述】:

每个 csv 文件有 2 个巨大的 (5-6 GB)。现在的目标是比较这两个文件。有多少行匹配,多少行不匹配?

假设 file1.csv 包含 5 个相似的行,我们需要将其计为 1 而不是 5。 同样,对于file2.csv,如果有冗余数据,我们需要将其计为1。

我希望输出显示匹配的行数和编号。不同的行数。

【问题讨论】:

  • 将文档数据库(MongoDb、CouchDB、...)中的两个文件作为两个集合加载,文件的每一行都有自己的哈希键。比较哈希键以找到匹配项。
  • 文件的顺序是否一致?
  • 如果它们相同但只是缺少第一行,@framontb 会起作用吗?
  • 它会找到没有顺序的相同行。文件中的第一行将匹配另一行的最后一行。
  • 说file1中有重复的行,如果这些重复的行在file2中出现的次数也相同,是否认为相同?您能否在描述中也添加所有这些案例。

标签: python python-3.x python-2.7


【解决方案1】:

我在 python 中编写了一个文件比较器,它可以优化地比较大文件并获得匹配的行数和不同的行数。用您的 2 个大文件替换 input_file1 和 input_file2 并运行它。让我知道结果。

input_file1 = r'input_file.txt'
input_file2 = r'input_file.1.txt'

__author__ = 'https://github.com/praveen-kumar-rr'

# Simple Memory Efficient high perfomance file comparer.
# Can be used to efficiently compare large files.

# Alogrithm:
# Hashes the lines and compared first.
# Non matching lines are picked as different count.
# All the matching lines are taken and the exact lines are read from file
# These strings undergo same comparison process based on string itself


def accumulate_index(values):
    '''
    Returns dict like key: [indexes]
    '''
    result = {}
    for i, v in enumerate(values):
        indexes = result.get(v, [])
        result[v] = indexes + [i]
    return result


def get_lines(fp, line_numbers):
    '''
    Reads lines from the file pointer based on the lines_numbers list of indexes
    '''
    return (v for i, v in enumerate(fp) if i in line_numbers)


def get_match_diff(left, right):
    '''
    Compares the left and right iterables and returns the matching and different items
    '''
    left_set = set(left)
    right_set = set(right)
    return left_set ^ right_set, left_set & right_set


if __name__ == '__main__':
    # Gets hashes of all lines for both files
    dict1 = accumulate_index(map(hash, open(input_file1)))
    dict2 = accumulate_index(map(hash, open(input_file2)))

    diff_hashes, matching_hashes = get_match_diff(
        dict1.keys(), dict2.keys())

    diff_lines_count = len(diff_hashes)

    matching_lines_count = 0
    for h in matching_hashes:
        with open(input_file1) as fp1, open(input_file2) as fp2:
            left_lines = get_lines(fp1, dict1[h])
            right_lines = get_lines(fp2, dict2[h])
            d, m = get_match_diff(left_lines, right_lines)
            diff_lines_count += len(d)
            matching_lines_count += len(m)

    print('Total number of matching lines is : ', matching_lines_count)
    print('Total number of different lines is : ', diff_lines_count)

【讨论】:

    【解决方案2】:

    我希望这个算法能工作

    1. 为两个文件中的每一行创建哈希
    2. 现在创建该哈希的集合
    3. 该集合的差和交集。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-05
      • 1970-01-01
      • 1970-01-01
      • 2022-11-04
      • 1970-01-01
      • 1970-01-01
      • 2020-03-09
      相关资源
      最近更新 更多