Python CSV - Combine, clean and to output emails in correct format
Question: I am trying to take more than 1 file with information such as names, email
addresses, and other. To take these files from CSV format and remove
everything except the emails. Then output a new file with delimiter of
semicolon all on same line. The final format should look like:

    
    
    someone@hotmail.com; someoneelse@gmail.com; someone3@university.edu
    

I must check the emails are in correct format of
`alphanumeric@alphanumeric.3letters`. I must remove all duplicates. I must
compare this list to another and remove the emails from 1 list that occur in
the others.

The final format will be such that someone can copy and paste into Outlook the
email recipient addresses.

I have looked at some video. Also here. I found: [python csv copy
column](http://stackoverflow.com/questions/19324968/python-csv-copy-column)

But I get an error when trying to write the new file. I have `import csv and
re`

Here is my code below:

    
    
    def final_emails(email_list):
      with open(email_list) as csv_file:
        read_csv = csv.reader(csv_file, delimiter=',')
    
        write_csv = csv.writer(out_emails, delimiter=";")
    
        for row in read_csv:
            email = row[2]  # only take the emails (from column 3)
            if email != '':  # remove empties
    
                # remove the header, or anything that doesn't have a '@'
                # convert to lowercase and append to list
                emails.append(re.findall('\w*@\w*.\w{3}', email.lower()))
                write_csv.write([email])
      return emails
    
    
    final_emails(list1)
    final_emails(list2)
    
    
    print(emails)
    

I have the print to check at bottom. I added write to make new file, but this
error TypeError: argument 1 must have a "write" method

I'm still trying to learn. I do many things this time I didn't before, like
csv and regular expression. Please any assistance. Thank you.


Answer: You need to define `out_emails` as file handle with writable permissions
before you can use it in `csv.writer`

`csv.writer` needs an object which has a `.write` property like file handles
to be able to write to it. It seems like `out_emails` doesn't have a `.write`
property.

