anurags
anurags

Reputation: 87

How do I find out the Country Code using text which contains city or area names? I need this information to format phone numbers to the E.164 format

I am parsing phone numbers into E.164 format for calling using Twilio API. I am using phonenumbers a python port of Google's libphonenumber.

In order to properly format a number without the international code into the E.164 format, I am parsing it like so:

x = phonenumbers.parse("020 8366 1177", "GB")
print(x)

However, I do not have any information about the number and the only data I can use is city/area data in .txt files. The city/area can be anything from the well known 'New York City', to an area like 'Gangnam, Seoul' or even just 'Gangnam'.

How do I obtain the country code ("GB", "US", "FR", "IN" etc) to properly format the phone numbers? What is the best approach?

I've looked at phonenumbers' geocoder, but geocoder accepts phonenumber objects which can only be obtained after parsing with a given country code. I also looked at MaxMind's GeoCity DB but it only allows you to obtain location information based on the IP address. IP address is not present in my data.

Upvotes: 0

Views: 663

Answers (1)

Elian Carsenat
Elian Carsenat

Reputation: 11

One of our clients had a similar issue, that they didn't have the Country ISO2 code to validate the phone number. They used the contact's name likely country of origin, to try and prioritize which country to try to validate to. For example, with a Korean name, they would try and validate to a Korean phone number. We've enhanced that use case with combining both the personal name (anthroponym), as well as the phone number formatting (the prefix, how many digits, how many spaces, if there is a dash or a parenthesis etc.)

It can be tried online here https://apiphonenumber.com/

It would be possible to do the same type of extension with a toponym such as 'Gangnam, Seoul' or even just 'Gangnam'.

Upvotes: 1

Related Questions