1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| """ Import """
import pandas as pd
import csv
import requests
import numpy as np
import xlrd
import sys
import pygeoip
import pycountry
gi = pygeoip.GeoIP("/home/vagrant/python/GeoIP.dat")
gic = pygeoip.GeoIP("/home/vagrant/python/GeoLiteCity.dat")
""" Function """
def IsoCountry(df):
countryiso_list = pd.Series()
cityiso_list = pd.Series()
j=0
for i in df.Country:
if i == "/":
countryiso_list.set_value = (j,"/")
else :
pays = pycountry.countries.get(alpha2=i)
pays1=pays.name
countryiso_list.set_value(j,pays1)
j = j+1
j=0
for i in df.Region:
if i == "/":
cityiso_list.set_value = (j,"/")
else:
ville = pycountry.subdivisions.get(code=str(df.Country[j])+"-"+i)
ville1=ville.name
cityiso_list.set_value(j,ville1)
if j % 10 == 0:
print j,
sys.stdout.flush()
j = j+1
df["Country_ISO"] = countryiso_list
df["City_ISO"] = cityiso_list
""" Script """
df=pd.read_excel(sys.argv[1],header=None, encoding='utf-8')
df.rename(columns={ 0 : 'Country', 1 : 'Region'}, inplace=True)
IsoCountry(df)
""" Sauvegarde """ |