pythonsmspython-phonenumber

How can I use the python-phonenumbers library to check the type of phone number?


How can we use the python-phonenumbers library to determine whether a particular phone number is a mobile number or landline number?


Solution

  • Use phonenumbers.phonenumberutil.number_type to get the number type (after you have parsed the number). e.g.

    x = phonenumbers.parse("0282784492", "AU")
    phonenumbers.phonenumberutil.number_type(x)
    

    So if you wanted to use x if it was not a fixed line number you could do:

    if phonenumbers.phonenumberutil.number_type(x) is not phonenumbers.PhoneNumberType.FIXED_LINE:
        # Do something...
    

    The possible phone number types are:

    See the full descriptions here.