def add_in_country_codes(self): """ Add in 2 and 3 letter country codes using pycountry module """ print("Adding in country codes") df = self.data.copy() JH_countries = df[['country_or_region']].copy().drop_duplicates() JH_countries['country_code_2'] = '' JH_countries['country_code_3'] = '' for index, row in JH_countries.iterrows(): country = row[0] #print(country) try: alpha_2 = countries.search_fuzzy(country)[0].alpha_2 alpha_3 = countries.search_fuzzy(country)[0].alpha_3 row['country_code_2'] = alpha_2 row['country_code_3'] = alpha_3 except LookupError: pass df = df.merge(JH_countries, how='left', on='country_or_region') self.data = df
def countrytoCode(country): # convert country to country code try: country_code = countries.search_fuzzy(country)[0].alpha_2 except LookupError: print("Couldn't find country try different one") return countrytoCode(userInput.read('Country')) return(country_code)
def countrytoCode(): # convert country to country code country = input("Country: ") try: country_code = countries.search_fuzzy(country)[0].alpha_2 except LookupError: print("Couldn't find country try different one") return countrytoCode() return (country_code)
def pandas_add_cc_3(row): """Pandas function for pulling 3-letter country code""" country = row['country'] #print(country) try: alpha_3 = countries.search_fuzzy(country)[0].alpha_3 return alpha_3 except LookupError: print("no dice") pass
def _get_countrycode(profile): """ Extract country code from addresses :param profile: contact profile :return: two-letter country code or None if not unambiguous """ countries_found = [] for addr in profile.get("addresses", []): if (addr.get("country", False) and addr["country"].strip().lower() not in countries_found): countries_found.append(addr["country"].strip().lower()) logging.debug("countries found %s", countries_found) if len(countries_found) == 1: # look up the country code return countries.search_fuzzy(countries_found[0])[0].alpha_2 return None
def geo_name_lookup(query): name = None alpha_2 = None try: query = query.title() alpha_2 = us_state_abbrev[query] name = query except KeyError: d_country = countries.get(name=query) if d_country: alpha_2 = d_country.alpha_2 name = d_country.name else: try: d_country = countries.search_fuzzy(query) alpha_2 = d_country[0].alpha_2 name = d_country[0].name except LookupError: alpha_2 = None return name, alpha_2
def _clean_address(self, address): # pylint: disable=too-many-branches """ clean up an address :param address: address dict :return: cleaned up address """ if address.get("country", ""): # check if the country is a valid country name # this also fixes two letter country codes try: country = countries.search_fuzzy( self._clean_country_name(address["country"]))[0].name except LookupError: country = False if country and country != address["country"]: address["country"] = country if re.match(r"^[a-zA-Z]{2}-[0-9]{4,6}$", address.get("zip", "")): # zipcode contains country code (e.g. CH-8005) countrycode, zipcode = address["zip"].split("-") try: address["country"] = countries.lookup(countrycode).name address["zip"] = zipcode except LookupError: pass if (not address.get("street", "") and not address.get("zip", "") and address.get("city", "")): # there is a city but no street/zip # logging.warning(address["city"].split(",")) if len(address["city"].split(",")) == 3: # Copenhagen Area, Capital Region, Denmark # Lausanne, Canton de Vaud, Suisse # Vienna, Vienna, Austria try: address["country"] = countries.search_fuzzy( self._clean_country_name( address["city"].split(",")[2]))[0].name address["city"] = self._clean_city_name( address["city"].split(",")[0]) except LookupError: pass elif len(address["city"].split(",")) == 2: # Zürich und Umgebung, Schweiz # Currais Novos e Região, Brasil # München und Umgebung, Deutschland # Région de Genève, Suisse # Zürich Area, Svizzera try: address["country"] = countries.search_fuzzy( self._clean_country_name( address["city"].split(",")[1]))[0].name address["city"] = self._clean_city_name( address["city"].split(",")[0]) except LookupError: pass elif len(address["city"].split(",")) == 1: # United States # Switzerland # Luxembourg try: address["country"] = countries.search_fuzzy( self._clean_country_name( self._clean_city_name(address["city"])))[0].name # if the lookup is successful this is a country name, not city name address["city"] = "" except LookupError: pass if address.get("type", "") not in ["Work", "Home", "Other"]: address["type"] = "Other" # ensure all relevant fields are set address["street"] = self._clean_street_name(address.get("street", "")) address["city"] = self._clean_city_name(address.get("city", "")) address["zip"] = address.get("zip", "").strip() address["state"] = address.get("state", "").strip() address["country"] = self._clean_country_name( address.get("country", "")) return address
def by_country_name(self, name: str) -> Iterator[OuiEntry]: try: return self.by_country_code( countries.search_fuzzy(name)[0].alpha_2) except LookupError: return []
def search(self, query): try: results = countries.search_fuzzy(query) except LookupError: results = [] return map(self.country_to_output, results)