|
10 | 10 | CHUNKSIZE = 10 ** 6 |
11 | 11 | CONNECTION_STRING = "sqlite:///{}".format(DATABASE_NAME) |
12 | 12 |
|
| 13 | + |
| 14 | +def _table_name_from_csv(filename: str) -> str: |
| 15 | + """Derive SQL table name from a CSV path (literal suffix, not str.strip).""" |
| 16 | + name = filename |
| 17 | + for suffix in (".csv.gz", ".csv"): |
| 18 | + if name.lower().endswith(suffix): |
| 19 | + name = name[: -len(suffix)] |
| 20 | + break |
| 21 | + return name.lower() |
| 22 | + |
| 23 | + |
13 | 24 | if os.path.exists(DATABASE_NAME): |
14 | 25 | msg = "File {} already exists.".format(DATABASE_NAME) |
15 | 26 | print(msg) |
16 | 27 | sys.exit() |
17 | 28 |
|
18 | 29 | for f in glob("*.csv.gz"): |
19 | 30 | print("Starting processing {}".format(f)) |
| 31 | + table = _table_name_from_csv(f) |
20 | 32 | if os.path.getsize(f) < THRESHOLD_SIZE: |
21 | 33 | df = pd.read_csv(f, index_col="ROW_ID") |
22 | | - df.to_sql(f.strip(".csv.gz").lower(), CONNECTION_STRING) |
| 34 | + df.to_sql(table, CONNECTION_STRING) |
23 | 35 | else: |
24 | 36 | # If the file is too large, let's do the work in chunks |
25 | 37 | for chunk in pd.read_csv(f, index_col="ROW_ID", chunksize=CHUNKSIZE): |
26 | | - chunk.to_sql( |
27 | | - f.strip(".csv.gz").lower(), CONNECTION_STRING, if_exists="append" |
28 | | - ) |
| 38 | + chunk.to_sql(table, CONNECTION_STRING, if_exists="append") |
29 | 39 | print("Finished processing {}".format(f)) |
30 | 40 |
|
31 | 41 | print("Should be all done!") |
0 commit comments