-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_db.py
More file actions
34 lines (25 loc) · 711 Bytes
/
Copy pathload_db.py
File metadata and controls
34 lines (25 loc) · 711 Bytes
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
import glob
from pathlib import Path
import duckdb
import pandas as pd
DUCKDB = "data_tests/data.duckdb"
def main():
"""
Load csv files into duckdb database.
"""
conn = duckdb.connect(database=str(DUCKDB), read_only=False)
files = glob.glob("data/*.csv")
for file in files:
table_name = Path(file).stem
df = pd.read_csv(file)
conn.register(table_name, df)
try:
conn.execute(
f"CREATE TABLE {table_name} AS SELECT * FROM {table_name}"
)
print(f"Table {table_name} created successfully.")
except Exception as e:
print(e)
conn.close()
if __name__ == "__main__":
main()