How to convert an area code into a state using PHP or python?
I have a phone number, including area code. Is there a PHP or python-accessible API or library that will return its 开发者_JAVA百科corresponding state?
For example:
415 -> CA
212 -> NY
Tab delimited copy/pasta for mapping Area codes to state abbreviations
I yanked these from here: http://www.bennetyee.org/ucsd-pages/area.html on 11/20/2014
They are correct so far as I can tell, if you see any wrong mappings, omissions, or incorrect mappings, do leave a comment and/or fix it.
201 NJ
202 DC
203 CT
204 MB
205 AL
206 WA
207 ME
208 ID
209 CA
210 TX
212 NY
213 CA
214 TX
215 PA
216 OH
217 IL
218 MN
219 IN
224 IL
225 LA
226 ON
228 MS
229 GA
231 MI
234 OH
236 BC
239 FL
240 MD
248 MI
250 BC
251 AL
252 NC
253 WA
254 TX
256 AL
260 IN
262 WI
267 PA
269 MI
270 KY
276 VA
278 MI
281 TX
283 OH
289 ON
301 MD
302 DE
303 CO
304 WV
305 FL
306 SK
307 WY
308 NE
309 IL
310 CA
312 IL
313 MI
314 MO
315 NY
316 KS
317 IN
318 LA
319 IA
320 MN
321 FL
323 CA
325 TX
330 OH
331 IL
334 AL
336 NC
337 LA
339 MA
340 VI
341 CA
343 ON
347 NY
351 MA
352 FL
360 WA
361 TX
365 ON
369 CA
380 OH
385 UT
386 FL
401 RI
402 NE
403 AB
404 GA
405 OK
406 MT
407 FL
408 CA
409 TX
410 MD
412 PA
413 MA
414 WI
415 CA
416 ON
417 MO
418 QC
419 OH
423 TN
424 CA
425 WA
430 TX
431 MB
432 TX
434 VA
435 UT
437 ON
438 QC
440 OH
442 CA
443 MD
450 QC
464 IL
469 TX
470 GA
475 CT
478 GA
479 AR
480 AZ
481 QC
484 PA
501 AR
502 KY
503 OR
504 LA
505 NM
506 NB
507 MN
508 MA
509 WA
510 CA
512 TX
513 OH
514 QC
515 IA
516 NY
517 MI
518 NY
519 ON
520 AZ
530 CA
539 OK
540 VA
541 OR
548 ON
551 NJ
557 MO
559 CA
561 FL
562 CA
563 IA
564 WA
567 OH
570 PA
571 VA
573 MO
574 IN
575 NM
579 QC
580 OK
585 NY
586 MI
587 AB
601 MS
602 AZ
603 NH
604 BC
605 SD
606 KY
607 NY
608 WI
609 NJ
610 PA
612 MN
613 ON
614 OH
615 TN
616 MI
617 MA
618 IL
619 CA
620 KS
623 AZ
626 CA
627 CA
628 CA
630 IL
631 NY
636 MO
639 SK
641 IA
646 NY
647 ON
650 CA
651 MN
657 CA
660 MO
661 CA
662 MS
669 CA
670 MP
671 GU
678 GA
679 MI
681 WV
682 TX
689 FL
701 ND
702 NV
703 VA
704 NC
705 ON
706 GA
707 CA
708 IL
709 NL
712 IA
713 TX
714 CA
715 WI
716 NY
717 PA
718 NY
719 CO
720 CO
724 PA
727 FL
731 TN
732 NJ
734 MI
737 TX
740 OH
747 CA
754 FL
757 VA
760 CA
762 GA
763 MN
764 CA
765 IN
769 MS
770 GA
772 FL
773 IL
774 MA
775 NV
778 BC
779 IL
780 AB
781 MA
782 NS
785 KS
786 FL
787 PR
801 UT
802 VT
803 SC
804 VA
805 CA
806 TX
807 ON
808 HI
810 MI
812 IN
813 FL
814 PA
815 IL
816 MO
817 TX
818 CA
819 QC
825 AB
828 NC
830 TX
831 CA
832 TX
835 PA
843 SC
845 NY
847 IL
848 NJ
850 FL
856 NJ
857 MA
858 CA
859 KY
860 CT
862 NJ
863 FL
864 SC
865 TN
867 YT
870 AR
872 IL
873 QC
878 PA
901 TN
902 NS
903 TX
904 FL
905 ON
906 MI
907 AK
908 NJ
909 CA
910 NC
912 GA
913 KS
914 NY
915 TX
916 CA
917 NY
918 OK
919 NC
920 WI
925 CA
927 FL
928 AZ
931 TN
935 CA
936 TX
937 OH
939 PR
940 TX
941 FL
947 MI
949 CA
951 CA
952 MN
954 FL
956 TX
957 NM
959 CT
970 CO
971 OR
972 TX
973 NJ
975 MO
978 MA
979 TX
980 NC
984 NC
985 LA
989 MI
Copy/paste these into a spreadsheet as columns A and B.
Here's how I specify a dictionary in python (edit this bit to specify your language's hashmap syntax)
area_code = {}
area_code['989'] = "MI"
area_code['985'] = "LA"
...
So paste this in Excel in Cell A3:
=CONCATENATE("area_code['", A1, "'] = '", B1, "'")
Which will stick your keys and values into code. Copy/paste this command from A3 to A100
Evaluate all. Copy/paste your results into the code file. Now you have a hashmap mapping area codes to state abbreviations.
http://www.50states.com/areacodes/
I suggest putting everything in an array in a seperate file:
// area_codes.php
$area_codes['205'] = 'AL';
$area_codes['251'] = 'AL';
$area_codes['256'] = 'AL';
$area_codes['334'] = 'AL';
$area_codes['907'] = 'AK';
// etc.
If you need the area code, just include the file and use it like this:
include('area_codes.php');
$area_code = '205';
echo $area_codes[$area_code]; // Output: 'AL'
As a non-American I used Google to look it up:
The easiest way would be to store the values in a database and just query it. ALternatively, you could have an array/list/... with the area codes in it and just use the languages features to retrieve the values needed.
Here is an easier-to-copy version of Eric's answer, just copy and paste into Python.
area_code_to_state = {'201': 'NJ',
'202': 'DC',
'203': 'CT',
'204': 'MB',
'205': 'AL',
'206': 'WA',
'207': 'ME',
'208': 'ID',
'209': 'CA',
'210': 'TX',
'212': 'NY',
'213': 'CA',
'214': 'TX',
'215': 'PA',
'216': 'OH',
'217': 'IL',
'218': 'MN',
'219': 'IN',
'224': 'IL',
'225': 'LA',
'226': 'ON',
'228': 'MS',
'229': 'GA',
'231': 'MI',
'234': 'OH',
'236': 'BC',
'239': 'FL',
'240': 'MD',
'248': 'MI',
'250': 'BC',
'251': 'AL',
'252': 'NC',
'253': 'WA',
'254': 'TX',
'256': 'AL',
'260': 'IN',
'262': 'WI',
'267': 'PA',
'269': 'MI',
'270': 'KY',
'276': 'VA',
'278': 'MI',
'281': 'TX',
'283': 'OH',
'289': 'ON',
'301': 'MD',
'302': 'DE',
'303': 'CO',
'304': 'WV',
'305': 'FL',
'306': 'SK',
'307': 'WY',
'308': 'NE',
'309': 'IL',
'310': 'CA',
'312': 'IL',
'313': 'MI',
'314': 'MO',
'315': 'NY',
'316': 'KS',
'317': 'IN',
'318': 'LA',
'319': 'IA',
'320': 'MN',
'321': 'FL',
'323': 'CA',
'325': 'TX',
'330': 'OH',
'331': 'IL',
'334': 'AL',
'336': 'NC',
'337': 'LA',
'339': 'MA',
'340': 'VI',
'341': 'CA',
'343': 'ON',
'347': 'NY',
'351': 'MA',
'352': 'FL',
'360': 'WA',
'361': 'TX',
'365': 'ON',
'369': 'CA',
'380': 'OH',
'385': 'UT',
'386': 'FL',
'401': 'RI',
'402': 'NE',
'403': 'AB',
'404': 'GA',
'405': 'OK',
'406': 'MT',
'407': 'FL',
'408': 'CA',
'409': 'TX',
'410': 'MD',
'412': 'PA',
'413': 'MA',
'414': 'WI',
'415': 'CA',
'416': 'ON',
'417': 'MO',
'418': 'QC',
'419': 'OH',
'423': 'TN',
'424': 'CA',
'425': 'WA',
'430': 'TX',
'431': 'MB',
'432': 'TX',
'434': 'VA',
'435': 'UT',
'437': 'ON',
'438': 'QC',
'440': 'OH',
'442': 'CA',
'443': 'MD',
'450': 'QC',
'464': 'IL',
'469': 'TX',
'470': 'GA',
'475': 'CT',
'478': 'GA',
'479': 'AR',
'480': 'AZ',
'481': 'QC',
'484': 'PA',
'501': 'AR',
'502': 'KY',
'503': 'OR',
'504': 'LA',
'505': 'NM',
'506': 'NB',
'507': 'MN',
'508': 'MA',
'509': 'WA',
'510': 'CA',
'512': 'TX',
'513': 'OH',
'514': 'QC',
'515': 'IA',
'516': 'NY',
'517': 'MI',
'518': 'NY',
'519': 'ON',
'520': 'AZ',
'530': 'CA',
'539': 'OK',
'540': 'VA',
'541': 'OR',
'548': 'ON',
'551': 'NJ',
'557': 'MO',
'559': 'CA',
'561': 'FL',
'562': 'CA',
'563': 'IA',
'564': 'WA',
'567': 'OH',
'570': 'PA',
'571': 'VA',
'573': 'MO',
'574': 'IN',
'575': 'NM',
'579': 'QC',
'580': 'OK',
'585': 'NY',
'586': 'MI',
'587': 'AB',
'601': 'MS',
'602': 'AZ',
'603': 'NH',
'604': 'BC',
'605': 'SD',
'606': 'KY',
'607': 'NY',
'608': 'WI',
'609': 'NJ',
'610': 'PA',
'612': 'MN',
'613': 'ON',
'614': 'OH',
'615': 'TN',
'616': 'MI',
'617': 'MA',
'618': 'IL',
'619': 'CA',
'620': 'KS',
'623': 'AZ',
'626': 'CA',
'627': 'CA',
'628': 'CA',
'630': 'IL',
'631': 'NY',
'636': 'MO',
'639': 'SK',
'641': 'IA',
'646': 'NY',
'647': 'ON',
'650': 'CA',
'651': 'MN',
'657': 'CA',
'660': 'MO',
'661': 'CA',
'662': 'MS',
'669': 'CA',
'670': 'MP',
'671': 'GU',
'678': 'GA',
'679': 'MI',
'681': 'WV',
'682': 'TX',
'689': 'FL',
'701': 'ND',
'702': 'NV',
'703': 'VA',
'704': 'NC',
'705': 'ON',
'706': 'GA',
'707': 'CA',
'708': 'IL',
'709': 'NL',
'712': 'IA',
'713': 'TX',
'714': 'CA',
'715': 'WI',
'716': 'NY',
'717': 'PA',
'718': 'NY',
'719': 'CO',
'720': 'CO',
'724': 'PA',
'727': 'FL',
'731': 'TN',
'732': 'NJ',
'734': 'MI',
'737': 'TX',
'740': 'OH',
'747': 'CA',
'754': 'FL',
'757': 'VA',
'760': 'CA',
'762': 'GA',
'763': 'MN',
'764': 'CA',
'765': 'IN',
'769': 'MS',
'770': 'GA',
'772': 'FL',
'773': 'IL',
'774': 'MA',
'775': 'NV',
'778': 'BC',
'779': 'IL',
'780': 'AB',
'781': 'MA',
'782': 'NS',
'785': 'KS',
'786': 'FL',
'787': 'PR',
'801': 'UT',
'802': 'VT',
'803': 'SC',
'804': 'VA',
'805': 'CA',
'806': 'TX',
'807': 'ON',
'808': 'HI',
'810': 'MI',
'812': 'IN',
'813': 'FL',
'814': 'PA',
'815': 'IL',
'816': 'MO',
'817': 'TX',
'818': 'CA',
'819': 'QC',
'825': 'AB',
'828': 'NC',
'830': 'TX',
'831': 'CA',
'832': 'TX',
'835': 'PA',
'843': 'SC',
'845': 'NY',
'847': 'IL',
'848': 'NJ',
'850': 'FL',
'856': 'NJ',
'857': 'MA',
'858': 'CA',
'859': 'KY',
'860': 'CT',
'862': 'NJ',
'863': 'FL',
'864': 'SC',
'865': 'TN',
'867': 'YT',
'870': 'AR',
'872': 'IL',
'873': 'QC',
'878': 'PA',
'901': 'TN',
'902': 'NS',
'903': 'TX',
'904': 'FL',
'905': 'ON',
'906': 'MI',
'907': 'AK',
'908': 'NJ',
'909': 'CA',
'910': 'NC',
'912': 'GA',
'913': 'KS',
'914': 'NY',
'915': 'TX',
'916': 'CA',
'917': 'NY',
'918': 'OK',
'919': 'NC',
'920': 'WI',
'925': 'CA',
'927': 'FL',
'928': 'AZ',
'931': 'TN',
'935': 'CA',
'936': 'TX',
'937': 'OH',
'939': 'PR',
'940': 'TX',
'941': 'FL',
'947': 'MI',
'949': 'CA',
'951': 'CA',
'952': 'MN',
'954': 'FL',
'956': 'TX',
'957': 'NM',
'959': 'CT',
'970': 'CO',
'971': 'OR',
'972': 'TX',
'973': 'NJ',
'975': 'MO',
'978': 'MA',
'979': 'TX',
'980': 'NC',
'984': 'NC',
'985': 'LA',
'989': 'MI'}
精彩评论