Untitled

 avatar
unknown
plain_text
a year ago
2.4 kB
6
Indexable
import pandas as pd
import numpy as np

def parse_excel_cell_values(excel_path, cells_info):
    """
    Parses values from specific cells in an Excel file.

    Parameters:
    - excel_path: Path to the Excel file.
    - cells_info: A list of dictionaries, each containing 'position' (a tuple of (row, column)),
                    'data_type' (desired Python data type), and 'fallback_value'.

    Returns:
    A list of parsed values corresponding to each cell information in cells_info.
    """
    # Load the Excel file into a DataFrame
    df = pd.read_excel(excel_path)
    
    # Initialize an empty list to store the parsed values
    parsed_values = []

    # Loop over the cells_info to process each specified cell
    for cell_info in cells_info:
        row, column = cell_info['position']
        desired_type = cell_info['data_type']
        fallback_value = cell_info['fallback_value']

        # Extract the value from the specified cell
        try:
            value = df.iloc[row, column]
        except IndexError:
            # If the specified cell is out of bounds, use the fallback value
            value = fallback_value

        # Apply parsing and type casting with fallback
        parsed_value = try_parse(value, desired_type, fallback_value)
        parsed_values.append(parsed_value)

    return parsed_values

def try_parse(value, data_type, fallback_value):
    """
    Attempts to parse a value into the specified data type, using a fallback value if parsing fails.

    Parameters:
    - value: The value to parse.
    - data_type: The desired Python data type.
    - fallback_value: The value to use if parsing fails.

    Returns:
    The parsed value or the fallback value.
    """
    if pd.isna(value) or value == '':
        return fallback_value

    try:
        return data_type(value)
    except (ValueError, TypeError):
        return fallback_value

# Example usage
if __name__ == "__main__":
    excel_path = 'path_to_your_excel_file.xlsx'
    cells_info = [
        {'position': (0, 0), 'data_type': str, 'fallback_value': 'default_string'},
        {'position': (1, 1), 'data_type': float, 'fallback_value': 0.0},
        # Add more cell specifications as needed
    ]

    parsed_values = parse_excel_cell_values(excel_path, cells_info)
    print(parsed_values)
Editor is loading...
Leave a Comment