A regex line to remove whitespaces unless within double quotes, taking into account escaped double quotes

I am parsing some game config files using Python and putting it all in dictionaries. It seemed to all work well until I encountered the following edge-case:

random_owned_controlled_state = {
    create_unit = { 
        division = "name = "6. Belarusian Red Riflemen" division_template = "Belarusian Red Riflemen" start_experience_factor = 0.5" 
        owner = PREV
    }
    create_unit = { 
        division = "name = "7. Belarusian Red Riflemen" division_template = "Belarusian Red Riflemen" start_experience_factor = 0.5" 
        owner = PREV
    }
}

I want to remove all spaces, tabs and newlines unless within doublequotes and eventually delimit separate statements with semicolons to get something like this which is easier to extract information from:

random_owned_controlled_state={create_unit={division="name = "6. Belarusian Red Riflemen" division_template = "Belarusian Red Riflemen" start_experience_factor = 0.5";owner=PREV;};create_unit={division="name = "7. Belarusian Red Riflemen" division_template = "Belarusian Red Riflemen" start_experience_factor = 0.5";owner=PREV;};};

I created this function to achieve this, or so I thought:

def remove_spacers(string: str, brackets: Tuple[str, str], operators: List[str], replacement: str, commentchar: str) -> str:
    """
    Get rid of all white spaces in file and remove or replace with replacement
    :param string:          Raw String
    :param replacement:     What to replace with
    :param commentchar:     Character used to indicate comments
    :return:                Cleaned String
    """
    # Strip front and end
    result = string.strip(" nt")

    # Remove all comments from text
    result = re.sub(rf"{commentchar}.*", "", result)

    # Remove all whitespace bundles except for inside "" and replace with single space
    result = re.sub(r"s+(?=([^"]*"[^"]*")*[^"]*$)", " ", result)

    # Remove whitespaces around operators (=,<,>)
    for operator in operators:
        result = re.sub(rf"s*{operator}s*", rf"{operator}", result)

    # Remove whitespaces after {
    result = re.sub(rf"{brackets[0]}s", rf"{brackets[0]}", result)

    # Replace whitespaces after anything else with ;
    result = re.sub(r"s(?=([^"]*"[^"]*")*[^"]*$)", replacement, result)

    # Replace any potential multiple consecutive ; with a single ;
    result = re.sub(rf"{replacement}{2,}", replacement, result)

    # Make sure every statement ends with ;
    result = re.sub(rf"(?<!{replacement}){brackets[1]}", rf"{replacement}{brackets[1]}", result)
    if result[-1] != replacement:
        result += replacement
    result = f"{{{result}}}{replacement}"
    return result

Calling it like: cleaned_text = remove_spacers(text, ('{', '}'), ['=', '<', '>'], ";", "#")

I likely need to adjust the following regex line:

result = re.sub(r"s+(?=([^"]*"[^"]*")*[^"]*$)", " ", result)

But am unsure how to achieve this behaviour since I am by no means experienced with regex.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật