Spaces:
Runtime error
Runtime error
File size: 1,142 Bytes
aaef24a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | #!/usr/bin/env python
import csv
import sqlite3
import sys
SCHEMA = """
CREATE TABLE penguins (
species text,
island text,
bill_length_mm real,
bill_depth_mm real,
flipper_length_mm real,
body_mass_g real,
sex text
);
"""
def main():
infile = sys.argv[1]
outfile = sys.argv[2]
con = sqlite3.connect(outfile)
con.execute(SCHEMA)
with open(infile, newline="") as f:
reader = csv.DictReader(f)
rows = [
(
row["species"],
row["island"],
float(row["bill_length_mm"]) if row["bill_length_mm"] else None,
float(row["bill_depth_mm"]) if row["bill_depth_mm"] else None,
float(row["flipper_length_mm"]) if row["flipper_length_mm"] else None,
float(row["body_mass_g"]) if row["body_mass_g"] else None,
row["sex"] if row["sex"] else None,
)
for row in reader
]
con.executemany(
"INSERT INTO penguins VALUES (?, ?, ?, ?, ?, ?, ?)", rows
)
con.commit()
con.close()
if __name__ == "__main__":
main()
|