Here's a well-structured Python script that reads .out files in the current directory, parses their contents, and organizes the extracted data into dictionaries as described.
✅ Features of the Script
- Extracts the program name from the first line using regex.
- Reads the first number as
proc (integer).
- Parses the second line containing the program name and two more integers:
inproc and iter.
- Extracts key-value pairs from subsequent lines, assuming the format
Key: Value....
- Collects all parsed data into a list of dictionaries (
all_data), one per file.
📜 Python Script
import os
import re
# List to store all parsed dictionaries
all_data = []
# Process each .out file in the current directory
for filename in os.listdir('.'):
if filename.endswith('.out'):
try:
with open(filename, 'r') as f:
lines = f.readlines()
# Ensure the file has enough lines
if len(lines) < 4:
print(f"Skipping {filename}: insufficient lines.")
continue
# Extract program name from the first line
match = re.search(r"'(.*?)'", lines[0])
if not match:
print(f"Skipping {filename}: invalid first line format.")
continue
program_name = match.group(1)
# Extract 'proc' from the second line
try:
proc = int(lines[1].strip())
except ValueError:
print(f"Skipping {filename}: invalid 'proc' value.")
continue
# Extract 'inproc' and 'iter' from the third line
parts = lines[2].strip().split()
if len(parts) < 3:
print(f"Skipping {filename}: invalid third line format.")
continue
try:
inproc = int(parts[1])
iter_val = int(parts[2])
except ValueError:
print(f"Skipping {filename}: invalid 'inproc' or 'iter' value.")
continue
# Initialize the dictionary for this file
data = {
'program': program_name,
'proc': proc,
'inproc': inproc,
'iter': iter_val
}
# Parse remaining lines for key-value pairs
for line in lines[3:]:
if ':' in line:
key, val_part = line.split(':', 1)
key = key.strip()
# Extract the first numeric token from the value part
val_tokens = val_part.strip().split()
if val_tokens:
val_str = val_tokens[0]
try:
val_num = float(val_str)
data[key] = val_num
except ValueError:
pass # Skip non-numeric values
# Add the completed dictionary to the list
all_data.append(data)
except Exception as e:
print(f"Error processing {filename}: {e}")
# Output the collected data (for demonstration)
for entry in all_data:
print(entry)
📌 Example Output (for each file)
Each dictionary will look like:
{
'program': 'prog_name',
'proc': 6,
'inproc': 5,
'iter': 200000,
'Runtime': 421.549374,
'I/O time': 246.0344,
'Throughput': 803.761587,
'IOPS': 292642.004295
}
Or for another file:
{
'program': 'otherprog',
'proc': 4,
'inproc': 4,
'iter': 3000,
'Runtime': 11.536746,
'I/O time': 195.944049,
'fsync share': 0.954957,
'Throughput': 15.136974,
'IOPS': 5511.777511
}
🧪 Usage
- Place the script in the same directory as your
.out files.
- Run it with:
python parse_out_files.py
- You can modify the final
print() to save the results to a file (e.g., JSON or CSV) if needed.
📎 Notes
- The script is robust against minor formatting inconsistencies.
- It gracefully skips malformed lines.
- You can enhance it by exporting the
all_data list to a structured format like JSON or CSV for external analysis.
Let me know if you'd like that added!