Skip to content

Instantly share code, notes, and snippets.

@3d24rd0
Last active April 8, 2022 10:07
Show Gist options
  • Save 3d24rd0/4b409a741819d572539fd7e5b8928e23 to your computer and use it in GitHub Desktop.
Save 3d24rd0/4b409a741819d572539fd7e5b8928e23 to your computer and use it in GitHub Desktop.
import pandas as pd
from tqdm import tqdm
import urllib.request
# data_url = 'https://raw.githubusercontent.com/inigoflores/ds-codigos-postales-ine-es/master/data/codigos_postales_municipios.csv'
data_url = 'https://raw.githubusercontent.com/inigoflores/ds-codigos-postales-ine-es/master/data/codigos_postales_municipios_entidades.csv'
response = urllib.request.urlretrieve(data_url, "codigos_postales.csv")
df = pd.read_csv(r'codigos_postales.csv', sep=',', low_memory=False)
provinces = {
'01': 'Álava',
'02': 'Albacete',
'03': 'Alicante',
'04': 'Almería',
'05': 'Ávila',
'06': 'Badajoz',
'07': 'Baleares',
'08': 'Barcelona',
'09': 'Burgos',
'10': 'Cáceres',
'11': 'Cádiz',
'12': 'Castellón',
'13': 'Ciudad Real',
'14': 'Córdoba',
'15': 'La Coruña',
'16': 'Cuenca',
'17': 'Gerona',
'18': 'Granada',
'19': 'Guadalajara',
'20': 'Guipúzcoa',
'21': 'Huelva',
'22': 'Huesca',
'23': 'Jaén',
'24': 'León',
'25': 'Lérida',
'26': 'La Rioja',
'27': 'Lugo',
'28': 'Madrid',
'29': 'Málaga',
'30': 'Murcia',
'31': 'Navarra',
'32': 'Orense',
'33': 'Asturias',
'34': 'Palencia',
'35': 'Las Palmas',
'36': 'Pontevedra',
'37': 'Salamanca',
'38': 'S.C. Tenerife',
'39': 'Cantabria',
'40': 'Segovia',
'41': 'Sevilla',
'42': 'Soria',
'43': 'Tarragona',
'44': 'Teruel',
'45': 'Toledo',
'46': 'Valencia',
'47': 'Valladolid',
'48': 'Vizcaya',
'49': 'Zamora',
'50': 'Zaragoza',
'51': 'Ceuta',
'52': 'Melilla',
}
def write_with_progress(df):
with open('postal.dart', 'w') as file:
file.write("""
class PostalCodeData {
final String? postalCode, provinceName, name;
const PostalCodeData({
required this.postalCode,
required this.provinceName,
required this.name,
});
}
const List<PostalCodeData> postalCodeList = [
""")
with tqdm(total=len(df)) as pbar:
for i in range(len(df)):
cp = str(df.iloc[i]['codigo_postal']).rjust(5, '0')
# name = str(df.iloc[i]['municipio_nombre'])
name = str(df.iloc[i]['nucleo_nombre'])
# provinceName = str(df.iloc[i]['municipio_nombre'])
if(name != "*Diseminado*"):
# file.write('const PostalCodeData(postalCode: "'+cp + '", provinceName: \'\'\''+name+'\'\'\'), \n')
file.write('const PostalCodeData(postalCode: "')
file.write(cp)
file.write('", provinceName:"')
file.write(provinces[cp[:2]])
file.write('", name:\'\'\'')
file.write(name)
file.write('\'\'\',), \n')
pbar.update(1)
tqdm._instances.clear()
file.write("];")
write_with_progress(df)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment