简体   繁体   中英

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?

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

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:

  • FIXED_LINE = 0
  • MOBILE = 1
  • FIXED_LINE_OR_MOBILE = 2
  • TOLL_FREE = 3
  • PREMIUM_RATE = 4
  • SHARED_COST = 5
  • VOIP = 6
  • PERSONAL_NUMBER = 7
  • PAGER = 8
  • UAN = 9
  • VOICEMAIL = 10
  • UNKNOWN = 99

See the full descriptions here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM