import re


def _prepare_row(row):
    """Flatten the nested dict for downstream export to CSV."""
    # Returned data includes fields from search result page and
    # data from detail page record for each layoff notice
    # the latter contains two key fields (address and number affected)
    detail = row.pop("detail")
    row["number_of_employees_affected"] = detail["number_of_employees_affected"]
    row["address"] = re.sub(r"\n+", "; ", detail["address"].strip())
    row["record_number"] = detail["record_number"]
    return row