def self_generate(output_filename, filename='iso3166-1.csv'):
    '\n    The following code can be used for self-generation of this file.\n\n    It requires a UTF-8 CSV file containing the short ISO name and two letter\n    country code as the first two columns.\n    '
    import csv
    import re
    countries = []
    alt_codes = []
    with open(filename, 'rb') as csv_file:
        for row in csv.reader(csv_file):
            name = row[0].decode('utf-8').rstrip('*')
            name = re.sub('\\(the\\)', '', name)
            if name:
                countries.append((name, row[1].decode('utf-8')))
                alt_codes.append((row[1].decode('utf-8'), row[2].decode('utf-8'), int(row[3])))
    with open(__file__, 'r') as source_file:
        contents = source_file.read()
    bits = re.match('(.*\nCOUNTRIES = \\{\n)(.*?)(\n\\}.*)', contents, re.DOTALL).groups()
    country_list = []
    for (name, code) in countries:
        name = name.replace('"', '\\"').strip()
        country_list.append('    "{code}": _("{name}"),'.format(name=name, code=code))
    content = bits[0]
    content += '\n'.join(country_list).encode('utf-8')
    alt_bits = re.match('(.*\nALT_CODES = \\{\n)(.*)(\n\\}.*)', bits[2], re.DOTALL).groups()
    alt_list = []
    for (code, code3, codenum) in alt_codes:
        name = name.replace('"', '\\"').strip()
        alt_list.append('    "{code}": ("{code3}", {codenum}),'.format(code=code, code3=code3, codenum=codenum))
    content += alt_bits[0]
    content += '\n'.join(alt_list).encode('utf-8')
    content += alt_bits[2]
    with open(output_filename, 'wb') as output_file:
        output_file.write(content)
    return countries
The following code can be used for self-generation of this file.

It requires a UTF-8 CSV file containing the short ISO name and two letter
country code as the first two columns.
def self_generate(output_filename, filename='iso3166-1.csv'):
    '\n    The following code can be used for self-generation of this file.\n\n    It requires a UTF-8 CSV file containing the short ISO name and two letter\n    country code as the first two columns.\n    '
    import csv
    import re
    countries = []
    alt_codes = []
    with open(filename, 'rb') as csv_file:
        for row in csv.reader(csv_file):
            name = row[0].decode('utf-8').rstrip('*')
            name = re.sub('\\(the\\)', , name)
            if name:
                countries.append((name, row[1].decode('utf-8')))
                alt_codes.append((row[1].decode('utf-8'), row[2].decode('utf-8'), int(row[3])))
    with open(__file__, 'r') as source_file:
        contents = source_file.read()
    bits = re.match('(.*\nCOUNTRIES = \\{\n)(.*?)(\n\\}.*)', contents, re.DOTALL).groups()
    country_list = []
    for (name, code) in countries:
        name = name.replace('"', '\\"').strip()
        country_list.append('    "{code}": _("{name}"),'.format(name=name, code=code))
    content = bits[0]
    content += '\n'.join(country_list).encode('utf-8')
    alt_bits = re.match('(.*\nALT_CODES = \\{\n)(.*)(\n\\}.*)', bits[2], re.DOTALL).groups()
    alt_list = []
    for (code, code3, codenum) in alt_codes:
        name = name.replace('"', '\\"').strip()
        alt_list.append('    "{code}": ("{code3}", {codenum}),'.format(code=code, code3=code3, codenum=codenum))
    content += alt_bits[0]
    content += '\n'.join(alt_list).encode('utf-8')
    content += alt_bits[2]
    with open(output_filename, 'wb') as output_file:
        output_file.write(content)
    return countries