Skip to content

Instantly share code, notes, and snippets.

@Saigesp
Created July 19, 2023 22:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Saigesp/0e353774c0c62973e21ba1d1fd0a192c to your computer and use it in GitHub Desktop.
Save Saigesp/0e353774c0c62973e21ba1d1fd0a192c to your computer and use it in GitHub Desktop.
Merge two csvs
import csv
from pathlib import Path
ROOT = Path(__file__).resolve().parent
INPUT_OLD = ROOT / "old.csv"
INPUT_NEW = ROOT / "new.csv"
OUTPUT = ROOT / "final.csv"
# csv data format:
# email,zip_code
# lorem@ipsum,12345
def merge():
old = {}
final = {}
with open(INPUT_OLD, "r") as inputold:
with open(INPUT_NEW, "r") as inputnew:
oldreader = csv.reader(inputold)
newreader = csv.reader(inputnew)
next(oldreader, None) # skip the headers
next(newreader, None)
for row in oldreader:
if row[1] and len(row[1]) in (4, 5) and row[1].isdigit(): # cp is valid
old[row[0]] = row[1].zfill(5)
for row in newreader:
if row[1] and len(row[1]) in (4, 5) and row[1].isdigit(): # cp is valid
continue
stored_cp = old.get(row[0])
if stored_cp:
final[row[0]] = stored_cp
with open(OUTPUT, "w") as output:
writer = csv.writer(output)
writer.writerow(["email", "zip_code"])
for k, v in final.items():
writer.writerow([k, v])
if __name__ == "__main__":
merge()
print("Done")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment