If-Else规则硬编码解析地址

问题描述 投票:-4回答:2

我想从Python开始使用基于规则的硬编码结构,最好使用IF-ELSE来解决以下问题:

例如,我的格式正确

UK postal address:
Flat 8, The Apartment, King Philip Street, SE1 3WX

从上述实际地址可以得出的不同变化是:

这些重点关注地址变体的第一行:

Flat 8 - Actual
8
F8
f8
flat 8
flat8
FLAT8
FLAT 8

这些重点关注地址变化的第二行:

The Apartment - Actual
Apartment, 
TheApartment
theapartment
the apartment

这些重点关注地址变体的第三行:

King Philip Street - Actual
King Philip St
King Philip st
King Philip street
King Philip STREET
king philip St
king philip st
king philip street
king philip STREET

这些重点关注地址变体的第四行:

SE1 3WX - Actual
SE13WX
SE1 3WX
se1 3wx
se13wx

因此,一旦地址输入到函数中,Python函数应该能够解析并输出上面的分段结果。

我有几千个这样的地址也可以解析。

有没有人之前做过这样的事情,有人可以帮我们告诉我这是如何实现的吗?

样本功能使用:

Python_Function("Flat 8, The Apartment, King Philip Street, SE1 3WX, England")

输出应该是:

第一行地址:

Flat 8
8
F8
f8
flat 8
flat8
FLAT8
FLAT 8

二线地址:

The Apartment
Apartment
TheApartment
theapartment
the apartment

第三行地址:

King Philip Street
King Philip St
King Philip st
King Philip street
King Philip STREET
king philip St
king philip st
king philip street
king philip STREET

第四行地址:

SE1 3WX
SE13WX
SE1 3WX
se1 3wx
se13wx

第五行地址:

England
england
eng
python regex if-statement
2个回答
1
投票

抛弃硬编码方法以获得更通用的方法。

我提供了开始(平面和公寓的逻辑)。希望你能自己完成剩下的工作。

import re
from itertools import product

digits_regex = re.compile('\d+')

address = "Flat 8, The Apartment, King Philip Street, SE1 3WX, England"

def generate(full_address):
    def generate_flat(flat_number, prefixes=('f', 'flat')):
        flat_options = [str(flat_number)]
        for prefix in prefixes:
            flat_options.append('{}{}'.format(prefix, flat_number))
            flat_options.append('{} {}'.format(prefix, flat_number))
            flat_options.append('{}{}'.format(prefix.upper(), flat_number))
            flat_options.append('{} {}'.format(prefix.upper(), flat_number))
        return flat_options

    def generate_apartment(apartment):
        prefix, *rest = apartment.split()
        joined = ''.join((prefix, *rest))
        return [apartment, rest[0], joined, joined.lower(), ' '.join((prefix.lower(), *map(str.lower, rest)))]

    flat, apartment, street, area, country = full_address.split(', ')

    return [', '.join(variation) for variation in product(generate_flat(digits_regex.findall(flat)[0]), generate_apartment(apartment))]


for variation in generate(address):
    print(variation)

产量

8, The Apartment
8, Apartment
8, TheApartment
8, theapartment
8, the apartment
f8, The Apartment
f8, Apartment
f8, TheApartment
f8, theapartment
f8, the apartment
f 8, The Apartment
f 8, Apartment
f 8, TheApartment
f 8, theapartment
f 8, the apartment
F8, The Apartment
F8, Apartment
F8, TheApartment
F8, theapartment
F8, the apartment
F 8, The Apartment
F 8, Apartment
F 8, TheApartment
F 8, theapartment
F 8, the apartment
flat8, The Apartment
flat8, Apartment
flat8, TheApartment
flat8, theapartment
flat8, the apartment
flat 8, The Apartment
flat 8, Apartment
flat 8, TheApartment
flat 8, theapartment
flat 8, the apartment
FLAT8, The Apartment
FLAT8, Apartment
FLAT8, TheApartment
FLAT8, theapartment
FLAT8, the apartment
FLAT 8, The Apartment
FLAT 8, Apartment
FLAT 8, TheApartment
FLAT 8, theapartment
FLAT 8, the apartment

-1
投票

这里有一些很有用的工具(没有在你的位置做任何事情):

for i,field in enumerate(input.split(', ')):
    lower_case = field.lower()
    upper_case = field.upper()
    capital = field.proper()
    without_spaces = field.replace(' ','') # you can remove spaces for above as well

    #then you can had specifics
    if i == 1:
        only_first_three = field[:3]

    #now if someones really writes weirdly:
    splitted_field = field.split(' ')
    random_capitals = [ word.proper() for word in splitter_fields if ...]
© www.soinside.com 2019 - 2024. All rights reserved.