Skip to content

Commit e71580c

Browse files
authored
Merge pull request #1 from plijtmaer/lab-02-ejemplo
Fix CSV UTF-8 BOM
2 parents 61886ab + d1e7f62 commit e71580c

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

scripts/load_postgres.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,24 @@ def run_sql(cur, path):
1515

1616

1717
def load_csv(cur, table, path, transform=None):
18-
with open(path, encoding="utf-8") as f:
18+
with open(path, encoding="utf-8-sig", newline="") as f:
1919
rows = list(csv.DictReader(f))
20+
2021
if transform:
2122
rows = [transform(r) for r in rows]
23+
2224
if not rows:
2325
return 0
26+
2427
cols = list(rows[0].keys())
2528
values = [[r[c] or None for c in cols] for r in rows]
29+
2630
execute_values(
2731
cur,
2832
f"INSERT INTO {table} ({','.join(cols)}) VALUES %s ON CONFLICT DO NOTHING",
2933
values,
3034
)
35+
3136
return len(rows)
3237

3338

0 commit comments

Comments
 (0)