def capitalize_country_name(cname):
    """Fixes capitalization edge cases in ISO 3166 country names"""
    #   Replace some non-ASCII character codes with ASCII equivalents.
    cname = cname.replace("\xc9", 'e')
    cname = cname.replace("\xd4", '0')
    cname = cname.replace("\xc5", 'a')

    tokens = [t.capitalize() for t in cname.split()]
    for (i, t) in enumerate(tokens):
        #   General cases.
        if t.upper().startswith('(U.'):
            tokens[i] = t.upper()
        elif t[0] == '(':
            tokens[i] = '(' + t[1:].capitalize()
        elif '-' in t:
            tokens[i] = '-'.join([e.capitalize() for e in t.split('-')])

        #   Some annoying special cases :-)
        if t.lower() in ('of', 'and', 'the', 'former'):
            tokens[i] = t.lower()
        elif t == "D'ivoire":
            tokens[i] = "D'Ivoire"
        elif t == "Mcdonald":
            tokens[i] = "McDonald"
        elif t.upper() == "U.S.":
            tokens[i] = "U.S."

    return ' '.join(tokens)
Fixes capitalization edge cases in ISO 3166 country names
recipes/Python/576768_Capitalizing_ISO_3166_country_names/recipe-576768.py