-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a script to create a sql file to update emails from csv list
- Loading branch information
1 parent
843bb6c
commit 576435c
Showing
2 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/usr/bin/env python | ||
|
||
############### | ||
# DESCRIPTION # | ||
############## | ||
# | ||
# This script will read a csv file that contains a list of emails to update, | ||
# with a column "old_email" that exist in the database, and a column | ||
# "new_email" with the value that we want to push to the database. | ||
# It will then generate a .sql file to update these values directly in the database. | ||
|
||
# we will use the generic csv module | ||
import csv | ||
|
||
CSV_PATH = './data/users-emails.csv' | ||
OUTPUT_PATH = './update-emails.sql' | ||
OLD_EMAIL_COLUMN = 'old_email' | ||
NEW_EMAIL_COLUMN = 'new_email' | ||
|
||
with open(OUTPUT_PATH, 'w') as outfile: | ||
pass # This just to clear any previous content | ||
|
||
# Note: use encoding='utf-8-sig' in the open() call if your file has BOM (Byte Order Mark) | ||
# Also make sure that the CSV file was saved as UTF-8 to avoid issues with special characters | ||
with open(CSV_PATH, newline='') as csvfile: | ||
# let's read the CSV using the standard "csv" library from python. No need for anything fancier. | ||
csvreader = csv.DictReader(csvfile, delimiter=',', quotechar='"') | ||
with open(OUTPUT_PATH, 'a') as outfile: | ||
# now we loop over each row in our CSV | ||
for row in csvreader: | ||
line = f"UPDATE users SET email = '{row[NEW_EMAIL_COLUMN]}' WHERE email = '{row[OLD_EMAIL_COLUMN]}';\n" | ||
outfile.write(line) | ||
|
||
print(f'SQL file written to {OUTPUT_PATH}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
old_email,new_email,another column | ||
[email protected],[email protected],Yes | ||
[email protected],[email protected],Nope | ||
[email protected],[email protected], | ||
[email protected],[email protected],This email doesn’t exist in the database |