-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataOrgScript.py
More file actions
56 lines (45 loc) · 2.12 KB
/
Copy pathDataOrgScript.py
File metadata and controls
56 lines (45 loc) · 2.12 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import csv
from collections import defaultdict
import os
import argparse
def reorganize_csv(input_file, output_file):
# Read the CSV file into a list
with open(input_file, newline='', encoding='utf-8') as csvfile:
reader = csv.reader(csvfile)
headers = next(reader) # Read the header row
rows = [row for row in reader if any(row)] # Remove completely empty rows
# Identify column indexes
run_idx = headers.index("Run")
test_idx = headers.index("Test")
# Remove rows where "Run" or "Test" are missing
rows = [row for row in rows if row[run_idx] and row[test_idx]]
# Maintain original test order as they first appear
seen_tests = []
test_groups = {}
for row in rows:
test_name = row[test_idx]
if test_name not in seen_tests:
seen_tests.append(test_name)
test_groups.setdefault(test_name, []).append(row)
# Write the reorganized data to a new CSV file
with open(output_file, mode="w", newline='', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(headers) # Write header row
for i, test_name in enumerate(seen_tests):
if i > 0:
writer.writerow([""] * len(headers)) # Add a spacer row
writer.writerows(test_groups[test_name])
print(f"Reorganized file saved as: {output_file}")
def main(folder_path):
output_folder = os.path.join(folder_path, 'Outputs')
os.makedirs(output_folder, exist_ok=True)
for file_name in os.listdir(folder_path):
if file_name.endswith('.csv'):
input_file_path = os.path.join(folder_path, file_name)
output_file_path = os.path.join(output_folder, file_name.replace('.csv', '_reorganized.csv'))
reorganize_csv(input_file_path, output_file_path)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Process all CSV files in a folder.')
parser.add_argument('folder_path', type=str, help='The folder containing CSV files to process')
args = parser.parse_args()
main(args.folder_path)