title: How can I merge two or more text files and remove duplicate email addresses with Python?
body: <p>I have two text files with space delimited email addresses - newalias.txt and origalias.txt. Essentially these are email alias mappings I want to merge together, but there are duplicates in the first index. I want to favor the line with a match in the first index in newalias.txt and drop the dup in origalias.txt. Also, drop exact duplicates.</p>
  
  <pre><code>OrigAlias:
  
      sam@example.com sam.smith@example.root.org
      jane@example.com jane.maiden@example.root.org
      bob@example.com robert.johnson@example.root.org
  
  NewAlias:
  
      sam@example.com samuel.smith@example.root.org
      jane@example.com jane.married@example.root.org
      bob@example.com robert.johnson@example.root.org
  
  Results:
  
      sam@example.com samuel.smith@example.root.org
      jane@example.com jane.married@example.root.org
      bob@example.com robert.johnson@example.root.org
  </code></pre>
  
  <p>I've been studying Python recently and I've done some interesting things, but text parsing is still a challenge for me. I'm still getting familiar with the options in Python.</p>
  
  <h2>Edit</h2>
  
  <p>I worked on the problem by myself for a while and came up with this:</p>
  
  <pre><code># Py 3.4.1
  # Instructions:
  # Rename current domain mapping export to dmapsOrig.txt
  # Rename whitespace delimited customer modifications file to dmapsNew.txt
  # Place the two text files and this script in the same directory
  # Run the script: 'python dmapsMerge.py'
  
  from datetime import date
  
  OrigDict = {}       # Create empty dictionaries for processing
  NewAddDict = {}     #
  ResultsDict = {}    #
  
  with open('dmapsOrig.txt', 'r') as file1:       # Populate OrigDict dictionary from dmapsOrig.txt file
      for x in file1:
          if not x.startswith("#"):               # Ignore commented lines
              dmaps = x.split()
              OrigDict[(dmaps[0])] = ''.join(dmaps[1])
  
  with open('dmapsNew.txt', 'r') as file2:        # Populate NewAddDict dictionary from dmapsNew.txt file
      for y in file2:
          if not y.startswith("#"):               # Ignore commented lines
              newdmaps = y.split()
              NewAddDict[(newdmaps[0])] = ''.join(newdmaps[1])
  
  with open('dmapsOrig-formatted-%s.txt' % date.today(), 'wt') as file3:
      file3.write('## Generated on %s' % date.today() + '\n') # Insert date stamp
      for alias in sorted(OrigDict.keys()):
          file3.write(alias + ' ' + OrigDict[alias] + '\n')   # Format original input and write to sorted file
  
  ResultsDict = OrigDict.copy()   # Copy OrigDict dictionary keys and values to ResultsDict Dictionary
  ResultsDict.update(NewAddDict)  # Merge new dmaps into original
  
  with open('dmapsResults-%s.txt' % date.today(), 'wt') as file4:
      file4.write('## Generated on %s' % date.today() + '\n')     # Insert date stamp
      for alias in sorted(ResultsDict.keys()):
          file4.write(alias + ' ' + ResultsDict[alias] + '\n')    # Format dictionary output and write to results.txt file
  
  file1.close() # Close open files
  file2.close() #
  file3.close() #
  file4.close() #
  </code></pre>