1

I have some code that creates a CSV file. The order of the rows it writes to the file can vary. I am writing a test to make sure the CSV file is what I expect. All I need to do is check that all rows are present and all fields equal. I have the code below, but am not sure how to get it to work so it doesn't care about the order of the rows. How can I make sure two CSV files contain the same rows, but the order of the rows doesn't matter?

 def assertRowsEqual(self, first, second)

    error_count = 0
    first_f = open(first)
    csv1 = csv.reader(first_f, delimiter=',', quotechar='"',
                    quoting=csv.QUOTE_ALL)

    second_f = open(second)
    csv2 = csv.reader(second_f, delimiter=',', quotechar='"',
                    quoting=csv.QUOTE_ALL)

    for row1 in csv1:
        row2 = csv2.next()
        if row1 != row2:
          self.fail("NOT THE SAME\n")
half full
  • 2,725
  • 3
  • 17
  • 8

2 Answers2

6

If you don't care about repeated rows:

set(csv1) == set(csv2)

else:

sorted(csv1) == sorted(csv2)
Fred Foo
  • 355,277
  • 75
  • 744
  • 836
0

If they'll fit in memory, just cast to lists and sort them.

Vamana
  • 580
  • 2
  • 7