-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonthly_minimal.py
52 lines (40 loc) · 1.42 KB
/
monthly_minimal.py
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
52
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import csv
import sqlite3
import sys
INPUT_PATH = "output/monthly.csv"
OUTPUT_PATH = "output/monthly-minimal.csv"
QUERY = "sql/monthly-minimal.sql"
def query_csv(input_csv, output_csv, sql_query):
# Read query from file
with open(sql_query, "r", encoding="utf-8") as file:
query = file.read()
# Create a memory SQLite DB
conn = sqlite3.connect(":memory:")
cursor = conn.cursor()
# Read CSV and create table
with open(input_csv, "r", encoding="utf-8") as csvfile:
reader = csv.reader(csvfile)
headers = next(reader)
cursor.execute(f"CREATE TABLE Monthly ({', '.join(headers)})")
cursor.executemany(
# trunk-ignore(bandit/B608): intended SQL injection
f"INSERT INTO Monthly VALUES ({', '.join(['?']*len(headers))})",
reader,
)
cursor.execute(query)
results = cursor.fetchall()
with open(output_csv, "w", encoding="utf-8", newline="") as csvfile:
writer = csv.writer(csvfile)
writer.writerow([description[0] for description in cursor.description])
writer.writerows(results)
conn.close()
try:
with open(INPUT_PATH, "r", encoding="utf-8") as f:
pass # Check if file exist
except FileNotFoundError:
print("Monthly list not found. Export via VNDB Query first.")
sys.exit()
# Query results
query_csv(INPUT_PATH, OUTPUT_PATH, QUERY)