Format undecorated phone number in django?

I have a full set of DB phone numbers as strings, they are all formatted as 1112223333, I would like to display it as 111-222-3333 in my django template.

I know what I can do

n = contacts.objects.get(name=name) n.phone = n.phone[:3] + '-' + n.phone[3:6] + '-' + n.phone[6:] 

but is there a better / more pythonic way?

+8
python string django
source share
5 answers

Just one other solution:

 n.phone = "%c%c%c-%c%c%c-%c%c%c%c" % tuple(map(ord, n.phone)) 

or

 n.phone = "%s%s%s-%s%s%s-%s%s%s%s" % tuple(n.phone) 
+7
source share

This might be redundant for your use case if all your numbers are formatted the same way, but you might consider using the phonenumbers module. This will allow you to easily add functionality (for example, international phone numbers, various formatting, etc.).

You can analyze your numbers as follows:

 >>> import phonenumbers >>> parsed_number = phonenumbers.parse('1112223333', 'US') >>> parsed_number PhoneNumber(country_code=1, national_number=1112223333L, extension=None, italian_leading_zero=False, country_code_source=None, preferred_domestic_carrier_code=None) 

Then, to format it the way you want, you can do this:

 >>> phonenumbers.format_number(parsed_number, phonenumbers.PhoneNumber()) u'111-222-3333' 

Please note that you can easily use other formats:

 >>> phonenumbers.format_number(parsed_number, phonenumbers.PhoneNumberFormat.NATIONAL) u'(111) 222-3333' >>> phonenumbers.format_number(parsed_number, phonenumbers.PhoneNumberFormat.INTERNATIONAL) u'+1 111-222-3333' >>> phonenumbers.format_number(parsed_number, phonenumbers.PhoneNumberFormat.E164) u'+11112223333' 
+13
source share

This was a bit late, but I decided that I would send the solution anyway. It is very simple and uses the creation of its own template tags (for use in your entire project). Another part of this is the use of parentheses around area code.

 from django import template register = template.Library() def phonenumber(value): phone = '(%s) %s - %s' %(value[0:3],value[3:6],value[6:10]) return phone register.filter('phonenumber', phonenumber) 

For the rest of your project, you only need {{ var|phonenumber }}

+8
source share

As we say Pythonic :), it is a good habit to always use join instead of adding ( + ) to concatenate strings :

 phone = n.phone n.phone = '-'.join((phone[:3],phone[3:6],phone[6:])) 
+6
source share
 def formatPhone(phone): formatted = '' i = 0 # clean phone. skip not digits phone = ''.join(x for x in phone if x.isdigit()) # set pattern if len(phone) > 10: pattern = 'X (XXX) XXX-XX-XX' else: pattern = 'XXX-XXX-XX-XX' # reverse phone = phone[::-1] pattern = pattern[::-1] # scan pattern for p in pattern: if i >= len(phone): break # skip non X if p != 'X': formatted += p continue # add phone digit formatted += phone[i] i += 1 # reverse again formatted = formatted[::-1] return formatted 

print formatPhone ('+ 7-111-222-33-44')

7 (111) 222-33-44

print formatPhone ('222-33-44')

222-33-44

print formatPhone ('23344')

2-33-44

+2
source share

All Articles