Skip to content

Commit 24dacb7

Browse files
ModellerModeller
Modeller
authored and
Modeller
committed
base_year: adding script to generate soundcast_inputs db
1 parent 777fcfa commit 24dacb7

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Create soundcast_inputs_db files from CSV records
2+
# These input files are generated from different scripts/notebooks in this directory or updated manually
3+
# Run this to generate
4+
5+
import pandas as pd
6+
import sqlite3
7+
import os
8+
import time
9+
10+
# Location of CSV input files to become database tables
11+
db_input_dir = r'R:\e2projects_two\SoundCast\Inputs\db_inputs'
12+
db_output_name = r'C:\workspace\soundcast_inputs_2023.db'
13+
14+
start_time = time.time()
15+
16+
conn = sqlite3.connect(db_output_name) # Creates or connects to the database file
17+
18+
# iterate through all CSV files in this directory and load them into the database via pandas dataframes
19+
for file_name in os.listdir(db_input_dir):
20+
if file_name.endswith('.csv'):
21+
print(file_name)
22+
# Use file name as table name
23+
table_name = os.path.splitext(file_name)[0]
24+
file_path = os.path.join(db_input_dir, file_name)
25+
df = pd.read_csv(file_path)
26+
df.to_sql(table_name, conn, if_exists='replace', index=False)
27+
28+
# Commit changes and close the connection
29+
conn.commit()
30+
conn.close()
31+
32+
end_time = time.time()
33+
elapsed_time = end_time - start_time
34+
print(f"Process completed in {elapsed_time:.2f} seconds")

0 commit comments

Comments
 (0)