There is a file with an **Excel ** spreadsheet containing the following type of data:
россия, московская область, ленинский район, г.видное, ул.заводская, д.6, кв.22
643,163000,78,,санктпетербург г,,офицерский пер,8б,,5
646602, россия, омская обл. , горьковский р-н, с. астыровка, ул. комсомольская, д. 17, кв. 1
656000, россия, г. барнаул, ул. эмилии алексеевой, д. 6, кв. 108
From such data, after passing through the program, you should get the following:
г. Видное, ул. Заводская, д.6, кв. 22
г. Санкт-Петербург, пер. Офицерский, д. 8Б, кв. 5
с. Астыровка, ул. Комсомольская, д. 17, кв. 1
г. Барнаул, ул. Эмилии Алексеевой, д. 6, кв. 108
I can’t imagine how to solve this, maybe there are some ready-made solutions in Excel or libraries in Python?
My attempt to write in Python (the result is 50/50):
def parse_address(address):
pattern = r'(?P<postal_code>d{6})?,?s*(?P<country>[^,]*),?s*(?P<region>[^,]*),?s*(?P<district>[^,]*),?s*(?P<locality>[^,]*),?s*(?P<street>[^,]*),?s*(?P<house>[^,]*),?s*(?P<apartment>[^,]*)'
# Убираем лишние пробелы и запятые
address = re.sub(r's*,s*', ',', address)
address = re.sub(r',+', ',', address)
address = address.strip(', ')
match = re.match(pattern, address)
if match:
address_dict = match.groupdict()
address_dict = {k: v.strip() if v else "Не указано" for k, v in address_dict.items()}
return address_dict
else:
return None
addresses = [
"россия, московская область, ленинский район, г.видное, ул.заводская, д.6, кв.22",
"643,163000,78,,санктпетербург г,,офицерский пер,8б,,5",
"646602, россия, омская обл. , горьковский р-н, с. астыровка, ул. комсомольская, д. 17, кв. 1",
"656000, россия, г. барнаул, ул. эмилии алексеевой, д. 6, кв. 108"
]
for address in addresses:
parsed_address = parse_address(address)
if parsed_address:
print("Распарсенный адрес:")
for key, value in parsed_address.items():
print(f"{key}: {value}")
else:
print("Не удалось распарсить адрес:", address)
1