File size: 1,426 Bytes
eeb9cbc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import csv

def filter_csv_by_api_and_status(csv_file_name, status_code, api_name):
    """
    Reads the given CSV file and prints rows where both the API and HTTP Status Code match the inputs.
    Args:
        csv_file_name (str): Path to the CSV file.
        status_code (str or int): HTTP status code to filter by.
        api_name (str): API name to filter by.
    """
    with open(csv_file_name, mode='r', newline='', encoding='utf-8') as csvfile:
        reader = csv.reader(csvfile)
        headers = next(reader)
        # Normalize headers to remove whitespace
        headers = [h.strip() for h in headers]
        api_idx = 0
        status_idx = 1
        count_index = 2
        found = False
        total_count = 0
        for row in reader:
            # Skip empty or malformed rows
            if len(row) < max(api_idx, status_idx) + 1:
                continue
            if api_name in row[api_idx].strip() and str(row[status_idx]).strip() == str(status_code):
                print({headers[api_idx]: row[api_idx], headers[status_idx]: row[status_idx], headers[2]: row[2] if len(row) > 2 else ''})
                found = True
                total_count += int(row[count_index])
        if not found:
            print('No matching rows found.')
        print(f'Total count: {total_count}')

# Example usage (uncomment to test):
filter_csv_by_api_and_status('csv/error-data.csv', 403, 'SimpleFlow')