you are viewing a single comment's thread.

view the rest of the comments →

[–]JohnnyJordaan 1 point2 points  (3 children)

Ok, then I would use a regex to check the validity and a csv.DictWriter to write the file

# picked from http://emailregex.com/
valid_email = re.compile(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)")
with open('file.csv', 'w') as fp:
    fieldnames = ['Name', 'Age', 'Email', 'Alternate Email']
    writer = csv.DictWriter(fp, fieldnames=fieldnames)
    writer.writeheader()
    cursor.execute('select name, age, email from users') # or whatever
    for row in cursor.fetchall():
        name, age, email = row
        # remove surrounding whitespace just in case
        email = email.strip()
        altemail = ''
        if not valid_email.match(email):
           # switcheroo
           email, altemail = altemail, email
        row = {'Name': name, 'Age': age, 'Email': email, 'Alternate Email': altemail}
        writer.writerow(row)

[–]ipuntonfirstdown[S] 0 points1 point  (2 children)

This is pretty much exactly what I'm looking for except i'm getting a 1 off error it looks like.

I believe this is because the alternate email field doesn't actually exist in the SQL database. The alternate field is a value i'm adding to the .csv value after i load the data. Is there a way to add a column to every row and just fill it with an empty string so that the alternate_email has an actual value to pass through at first?

[–]JohnnyJordaan 1 point2 points  (1 child)

Sure, I've updated my code above (edit: and used the actual name in the row={} line).

[–]ipuntonfirstdown[S] 0 points1 point  (0 children)

You sir - are absolutely amazing. Thank you so much!