andyhughes73
andyhughes73

Reputation: 91

Trying to e.164 a phone number from form input

I'm trying to take a UK mobile phone number input from a web form and use Python to clean it into a E.164 format, then validate it, before entering it into a database.

The library I'm trying to use is "Phonenumbers" and the code I'm experimenting with so far is:

def Phone():
    my_number = '+4407808765066'
    clean_phone = phonenumbers.parse(my_number, "GB")
    cleaner_phone = phonenumbers.format_number(clean_phone, 
    phonenumbers.PhoneNumberFormat.E164)
    valid = phonenumbers.is_possible_number(cleaner_phone)
    print(cleaner_phone)

Just working through the logic, my expectation is that it should take the contents of my_number variable, format it through into the clean_phone variable, then format it to E.164 standard before passing it to the validation and return the output to valid. The print statement is for me to see the output.

Everything looks to work ok if I comment out the valid variable line. As soon as I uncomment it, I get an error (see below).

Traceback (most recent call last):
  File "phone_test.py", line 14, in <module>
    Phone()
  File "phone_test.py", line 10, in Phone
    valid = phonenumbers.is_possible_number(cleaner_phone)
  File "D:\Dropbox\Coding Projects\learner_driver_app\env\lib\site-packages\phonenumbers\phonenumberutil.py", line 2257, in is_possible_number      
    result = is_possible_number_with_reason(numobj)
  File "D:\Dropbox\Coding Projects\learner_driver_app\env\lib\site-packages\phonenumbers\phonenumberutil.py", line 2358, in is_possible_number_with_reason
    return is_possible_number_for_type_with_reason(numobj, PhoneNumberType.UNKNOWN)
  File "D:\Dropbox\Coding Projects\learner_driver_app\env\lib\site-packages\phonenumbers\phonenumberutil.py", line 2393, in is_possible_number_for_type_with_reason
    national_number = national_significant_number(numobj)
  File "D:\Dropbox\Coding Projects\learner_driver_app\env\lib\site-packages\phonenumbers\phonenumberutil.py", line 1628, in national_significant_number
    if numobj.italian_leading_zero:
AttributeError: 'str' object has no attribute 'italian_leading_zero'

Where am I going wrong?

Upvotes: 1

Views: 3725

Answers (2)

white
white

Reputation: 584

Your my_number is a variable of type str (string), thus the last line of your error). The string class does not know the attribute national_number.
Reading through their examples on GitHub, I suspect you need to pass your string through the parse() function first before you can use functions from the library.

def phone_parser():
    my_number = '+4407811111111'
    number_parsed = phonenumbers.parse(my_number, None)  # this is new
    clean_phone = phonenumbers.format_number(number_parsed, 
    phonenumbers.PhoneNumberFormat.E164)
    return clean_phone

The None in parse() may be replaced by a country code if it is known. Otherwise, it will try to figure it out but may fail.

Edit to account for more information in the original question:
Apparently phonenumbers.format_number() returns a string, therefore you have to re-parse the number again to get an object of type phonenumbers.phonenumber.PhoneNumber (you can check the type of objects with type(my_object)). After that, your code will return True.

Upvotes: 5

mkrieger1
mkrieger1

Reputation: 23245

You can't use the format_number function with a string as argument, it expects a PhoneNumber object.

You can get one by using the parse function.

See https://github.com/daviddrysdale/python-phonenumbers/tree/dev/python#example-usage

Upvotes: 0

Related Questions