File size: 897 Bytes
5983c9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import pandas as pd
from db.db_utils import get_connection

db_conn = get_connection()
cursor = db_conn.cursor()

# Fetch distinct run_keys
cursor.execute("SELECT DISTINCT run_key FROM results")
run_keys = cursor.fetchall()

for run_key in run_keys:
    run_key_value = run_key[0]

    # Query to get the data grouped by run_key and ordered by run_row
    query = f"""
    SELECT * 
    FROM results 
    WHERE run_key = %s 
    ORDER BY run_row;
    """
    
    cursor.execute(query, (run_key_value,))
    rows = cursor.fetchall()

    # Get column names
    colnames = [desc[0] for desc in cursor.description]

    # Create a DataFrame
    df = pd.DataFrame(rows, columns=colnames)

    # Export DataFrame to CSV
    csv_filename = f"{run_key_value}_parsed.csv"
    df.to_csv(f"./results/{csv_filename}", index=False)

# Close the cursor and connection
cursor.close()
db_conn.close()