title: How can you keep track of revisions in a csv file with a Python program?
body: <p>I have a CSV file where each row has an ID followed by several attributes. Initially, my task was to find the IDs with matching attributes and put them together as a family. Then, outputting them in another CSV document under the format of every relationship printed in different rows.</p>
  
  <p>The basic outline for the CSV file looks like this:</p>
  
  <pre class="lang-none prettyprint-override"><code>ID     SIZE    SPEED    RANK
  123     10      20       2
  567     15      30       1
  890     10      20       2
  321     20      10       3
  295     15      30       1
  </code></pre>
  
  <p>The basic outline for the python module looks like this:</p>
  
  <pre><code>FAMILIES = {}
  ATTRIBUTES = ['ID', 'SIZE', 'SPEED', 'RANK']
  with open('data.csv', 'rb') as f:
    data = csv.DictReader(f)
    for row in data:
      fam_id = str(tuple([row[field_name] for field_name in ATTRIBUTES]))
      id = row['ID']
      FAMILIES.setdefault(fam_id, [])
      FAMILIES[fam_id].append(id)
  
  output = []
  for fam_id, node_arr in FAMILIES.items():
      for from_item in node_arr:
          for to_item in node_arr:
              if from_item != to_item:
                  output.append(fam_id, from_item, to_item)
  
  def write_array_to_csv(arr):
      with open('hdd_output_temp.csv', 'wb') as w:
          writer = csv.writer(w)
          writer.writerows(arr)
  
  if __name__ == "__main__":
      write_array_to_csv(output)
  </code></pre>
  
  <p>Which would print into a CSV like this:</p>
  
  <pre class="lang-none prettyprint-override"><code>('10,20,2')    123    890
  ('10,20,2')    890    123
  ('15,30,1')    567    295
  ('15,30,1')    295    567
  </code></pre>
  
  <p>Now, my question is, if I were to go into the original csv file and make some revisions, how could I alter the code to detect all the updated relationships. I would like to put all the added relationships into FAMILIES2 and all the broken relationships into FAMILIES3. So if a new ID '589' were added that matched the '20,10,3' family and '890' was updated to have a different ID of '10,20,1',
  I would like FAMILIES 2 to be able to output:</p>
  
  <pre class="lang-none prettyprint-override"><code>('20,10,3')    321    589
  ('20,10,3')    589    321
  </code></pre>
  
  <p>And FAMILIES3 to output:</p>
  
  <pre class="lang-none prettyprint-override"><code>('10,20,2')    123    890
  ('10,20,2')    890    123
  </code></pre>