#!/usr/bin/env python
# -*- coding=utf-8 -*-
import re
import sys
import gzip
numberRe = re.compile('^1[0-9]{10}$')
numsDbRe = re.compile('(^1[0-9]{6}) (.*) (.*)')
def getRegion(number,numsDb):
if not numberRe.search(number):
return '未知,未知'
if not numsDb:
return '未知,未知'
k = number[0:3]
v = number[3:7]
if not k in numsDb:
return '未知,未知'
else:
region = numsDb[k].get(v,('未知','未知'))
return ','.join(region)
def loadRegion():
RegionDb = {}
try:
for line in gzip.open('numsdb.tar.gz').readlines():
line = line.strip()
m = numsDbRe.search(line)
if m:
nums,area,card = m.groups()
k = nums[0:3]
v = nums[3:7]
if k in RegionDb:
RegionDb[k][v] = (area,card)
else:
RegionDb[k]={}
RegionDb[k][v] = (area,card)
return RegionDb
except Exception,e:
print e
return {}
if __name__ == '__main__':
numsDb = loadRegion()
try:
number = sys.argv[1]
print number,getRegion(number,numsDb)
except:
pass
|