I have the file which contains the list of telephone number ranges and their owners (names of mobile operators) - http://www.rossvyaz.ru/opendata/7710549038-Rosnumbase/Kody_DEF-9kh.csv:
900 1940000 1949999 10000 Sky-1800
916 0 9999999 10000000 Mobile TeleSystems
917 0 29999 30000 Mobile TeleSystems
And I will have new phone numbers each week (in the format like +79161234567). So, I should detect their operators. So, I am planning to download updated list each week and then to match phones I have against this list. The main question is how to do it effectively. Once I've downloaded the file, what is the best way to keep that in memory and then search for the mobile operator?
The first idea is to read the file line by line, parse it, compare DEF (if '916' == def_from_the_line), if so, then compare the range (if 1234567>=range_start_from_the_line and 1234566<=range_end_from_the_line), but it will not be quite effective (taken into consideration that I will have to look for several phone numbers).
Here is a data structure that you could use:
from collections import defaultdict
import pickle
operators = defaultdict(list)
for line in open('data').readlines():
pre, begin, end, _, operator_name = line.split(None,4)
operators[pre].append((int(begin),int(end),operator_name))
pickle.dump(operators, open("operators", "wb"))
# # # # # # #
operators = pickle.load(open("operators", "r"))
def get_operator(number, operators):
pre = number[2:5]
suf = int(number[5:])
for begin, end, name in operators[pre]:
if begin <= suf <= end:
return name.strip()
print get_operator("+79161234567", operators)
The above prints Mobile TeleSystems
I have the file which contains the list of telephone number ranges and their owners (names of mobile operators) - http://www.rossvyaz.ru/opendata/7710549038-Rosnumbase/Kody_DEF-9kh.csv:
900;1940000;1949999;10000;Sky-1800
916;0;9999999;10000000;Mobile TeleSystems
917;0;29999;30000;Mobile TeleSystems
And I will have new phone numbers each week (in the format like +79161234567). So, I should detect their operators. So, I am planning to download updated list each week and then to match phones I have against this list. The main question is how to do it effectively. Once I've downloaded the file, what is the best way to keep that in memory and then search for the mobile operator?
The first idea is to read the file line by line, parse it, compare DEF (if '916' == def_from_the_line), if so, then compare the range (if 1234567>=range_start_from_the_line and 1234566<=range_end_from_the_line), but it will not be quite effective (taken into consideration that I will have to look for several phone numbers).