@@ -2,31 +2,42 @@ phonenumbers Python Library
22===========================
33
44This is a Python port of libphonenumber, originally from:
5- http://code.google.com/p/libphonenumber/ .
5+ [ http://code.google.com/p/libphonenumber/ ] ( http://code.google.com/p/libphonenumber/ ) .
66
7- Original Java code is Copyright (C) 2009-2011 Google Inc.
7+ Original Java code is Copyright (C) 2009-2011 The Libphonenumber Authors
88
99Example Usage
1010-------------
1111
12+ The main object that the library deals with is a ` PhoneNumber ` object. You can create this from a string
13+ representing a phone number using the ` parse ` function, but you also need to specify the country that the
14+ phone number is from.
15+
1216 >>> import phonenumbers
1317 >>> x = phonenumbers.parse("+442083661177", None)
1418 >>> print x
1519 Country Code: 44 National Number: 2083661177 Leading Zero: False
1620 >>> type(x)
1721 <class 'phonenumbers.phonenumber.PhoneNumber'>
22+ >>> y = phonenumbers.parse("020 8366 1177", "GB")
23+ >>> print y
24+ Country Code: 44 National Number: 2083661177 Leading Zero: False
25+ >>> x == y
26+ True
27+
28+ Once you've got a phone number, a common task is to format it in a standardized format. There are a few
29+ formats available (under ` PhoneNumberFormat ` ), and the ` format_number ` function does the formatting.
30+
1831 >>> phonenumbers.format_number(x, phonenumbers.PhoneNumberFormat.NATIONAL)
1932 u'020 8366 1177'
2033 >>> phonenumbers.format_number(x, phonenumbers.PhoneNumberFormat.INTERNATIONAL)
2134 u'+44 20 8366 1177'
2235 >>> phonenumbers.format_number(x, phonenumbers.PhoneNumberFormat.E164)
2336 u'+442083661177'
24- >>> y = phonenumbers.parse("020 8366 1177", "GB")
25- >>> print y
26- Country Code: 44 National Number: 2083661177 Leading Zero: False
27- >>> x == y
28- True
29- >>>
37+
38+ If your application has a UI that allows the user to type in a phone number, it's nice to get the formatting
39+ applied as the user types. The ` AsYouTypeFormatter ` object allows this.
40+
3041 >>> formatter = phonenumbers.AsYouTypeFormatter("US")
3142 >>> print formatter.input_digit("6")
3243 6
@@ -48,18 +59,26 @@ Example Usage
4859 (650) 253-222
4960 >>> print formatter.input_digit("2")
5061 (650) 253-2222
51- >>>
62+
63+ Sometimes, you've got a larger block of text that may or may not have some phone numbers inside it. For this,
64+ the ` PhoneNumberMatcher ` object provides the relevant functionality; you can iterate over it to retrieve a
65+ sequence of ` PhoneNumberMatch ` objects.
66+
5267 >>> text = "Call me at 510-748-8230 if it's before 9:30, or on 703-4800500 after 10am."
5368 >>> for match in phonenumbers.PhoneNumberMatcher(text, "US"):
5469 ... print match
55- ...
70+ ...
5671 PhoneNumberMatch [11,23) 510-748-8230
5772 PhoneNumberMatch [51,62) 703-4800500
5873 >>> for match in phonenumbers.PhoneNumberMatcher(text, "US"):
5974 ... print phonenumbers.format_number(match.number, phonenumbers.PhoneNumberFormat.E164)
60- ...
75+ ...
6176 +15107488230
6277 +17034800500
78+
79+ Finally, you might want to get some information about the location that corresponds to a phone number. The
80+ ` geocoder.area_description_for_number ` does this, when possible.
81+
6382 >>> from phonenumbers.geocoder import area_description_for_number
6483 >>> ch_number = phonenumbers.parse("0431234567", "CH")
6584 >>> print repr(area_description_for_number(ch_number, "de"))
@@ -70,13 +89,15 @@ Example Usage
7089 u'Zurich'
7190 >>> print repr(area_description_for_number(ch_number, "it"))
7291 u'Zurigo'
73- >>>
92+
93+ For more information about the other functionality available from the library, look in the unit tests or in the original
94+ [ libphonenumber project] ( http://code.google.com/p/libphonenumber/ ) .
7495
7596Project Layout
7697--------------
77- * The python/ directory holds the Python code.
78- * The resources/ directory is a copy of the resources/
79- directory from libphonenumber. This is not needed
80- to run the Python code, but is needed when upstream
81- changes to the master XML metadata need to be
82- incorporated.
98+ * The ` python/ ` directory holds the Python code.
99+ * The ` resources/ ` directory is a copy of the ` resources/ `
100+ directory from
101+ [ libphonenumber ] ( http:// code.google.com/p/libphonenumber/source/browse/#svn%2Ftrunk%2Fresources ) .
102+ This is not needed to run the Python code, but is needed when upstream
103+ changes to the master XML metadata need to be incorporated.
0 commit comments