Skip to content Skip to sidebar Skip to footer

Compare Lines In 2 Text Files

I have two large text files (200,000+ lines), CSV format. I need to compare them, line by line, but the fields maybe switched within each line. Example file A.csv: AAA,BBB,,DDD E

Solution 1:

You can normalize the check, without affecting the data.

withopen('big1.csv') as i, open('big2.csv') as j:
   a = csv.reader(i)
   b = csv.reader(j)
   for linea in a:
      lineb = next(b)
      ifsorted(map(str.lower, linea)) != sorted(map(str.lower, lineb)):
          print('{} does not match {}'.format(linea, lineb))

Solution 2:

Try using diff as a Linux/Unix command line - this is very useful for comparing files.

Post a Comment for "Compare Lines In 2 Text Files"