Last active
August 29, 2015 14:21
-
-
Save osya/90b5f43588235bdd6e62 to your computer and use it in GitHub Desktop.
Парсилка кадастровых номеров на Python #Python #cadastral
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def torgiGetLandNumber(s): | |
''' | |
:param s: строка, содержащая кадастровый номер | |
:return: возвращает распарсенный кадастровый номер из входной строки | |
''' | |
# Делаем рекурсивную замену символов в строке | |
x2 = clear_cell(s) | |
x2 = x2.replace('"','').replace('-',':').replace('/',':').replace('\\',':').replace(';',':').replace('.',':').strip() | |
x2 = (lambda s: (lambda f, *a: f(f, *a)) (lambda rec, s: rec(rec, s.replace('::',':')) if '::' in s else s, s))(x2) # рекурсивно заменяем повторно идущие двоеточия | |
# Вырезаем из строки все цифры и символы двоеточия | |
res = ''.join([x for x in x2 if x.isdigit() or ':' == x]) | |
res = (lambda s: (lambda f, *a: f(f, *a)) (lambda rec, s: rec(rec, s.replace('::',':')) if '::' in s else s, s))(x2) # рекурсивно заменяем повторно идущие двоеточия | |
if ':' != res: | |
return res | |
else: | |
# парсим регулярными выражениями | |
pat = ur'\d{1,2}:\d{1,3}:\d{1,7}:\d{1,35}:\d{1,5}|\d{1,2}:\d{1,3}:\d{1,7}:\d{1,35}|\d{1,2}:\d{1,3}:\d{1,7}|\d{1,2}:\d{1,3}' | |
res = re.search(pat, x2.replace(' ',':')) | |
if res: | |
return res.group(0) | |
else: | |
# Разбиваем по двоеточиям | |
DistrictNum = ''; AreaNum = ''; BlockNum = ''; ParcelNum = ''; OKS = '' | |
x3 = x2.replace(':','') | |
DistrictNum = getDigitsFromString(x3[0:2]) | |
if DistrictNum: | |
AreaNum = getDigitsFromString(x3[2:4]) | |
if AreaNum: | |
BlockNum = getDigitsFromString(x3[4:11]) | |
if BlockNum: | |
ParcelNum = getDigitsFromString(x3[11:46]) | |
if ParcelNum: | |
OKS = getDigitsFromString(x3[46:51]) | |
if ParcelNum: | |
return '%s:%s:%s:%s' % (DistrictNum, AreaNum, BlockNum, ParcelNum) | |
elif BlockNum: | |
return '%s:%s:%s' % (DistrictNum, AreaNum, BlockNum) | |
elif AreaNum: | |
return '%s:%s' % (DistrictNum, AreaNum) | |
elif DistrictNum: | |
return '%s' % (DistrictNum) | |
elif OKS: | |
return '%s' % (OKS) | |
else: | |
return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment