-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLite2CSV.py
More file actions
39 lines (31 loc) · 1.21 KB
/
SQLite2CSV.py
File metadata and controls
39 lines (31 loc) · 1.21 KB
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 sqlite3
import csv
from collections import defaultdict
# Function to extract the base image name
def extract_image_name(full_name):
parts = full_name.split('_cell_')
return parts[0] + '.tif'
# Connect to the SQLite database
db_path = './experiment/extracted' # Path to where the SQLite database from the second Cell Profiler run is
connection = sqlite3.connect(db_path)
cursor = connection.cursor()
# Fetch all rows from the table
table_name = 'MyExpt_Per_Image'
cursor.execute(f"SELECT * FROM {table_name}")
rows = cursor.fetchall()
# Group rows by image name
grouped_data = defaultdict(list)
column_index = 9 # REPLACE with the index of 'Image_FileName_AllExtractedImages' in your table
for row in rows:
image_name = extract_image_name(row[column_index])
grouped_data[image_name].append(row)
# Export each group to a separate CSV file
for image_name, data in grouped_data.items():
with open(f"{image_name}.csv", 'w', newline='') as csv_file:
writer = csv.writer(csv_file)
# Write the headers first
writer.writerow([description[0] for description in cursor.description])
# Write the content
writer.writerows(data)
# Close the SQLite connection
connection.close()