0

设置

我有包含英国格式地址的字符串,例如address = '6A McCarthy Way'.

我需要从地址中获取门牌号,例如house_number = '6A


当前代码

我有以下工作代码,

position = re.search('\d+', address).start()

if position == 0:
    for i in range(0,100000):    
        if address[position + i] != ' ':
            house_number = address[:position + i + 1]    
        else:
            break
else:
    house_number = address[position:]     

对于address = '6A McCarthy Way'and address = 'McCarthy Way 6A',代码都返回house_number = '6A'


问题

此代码假定

  • 门牌号将在开头或结尾address
  • 门牌号和地址将仅采用上述 2 种格式 - 例如从不address = '6A, McCarthy Way'address = '6 McCarthy Way'
  • 没有错误address——例如从不address = '6AMcCarthy Way'

最后,即使假设适用于所有情况,我也不确定这是最 Pythonic 的方式。

如何改进代码?

4

1 回答 1

3

使用re.search

import re
address = '6A McCarthy Way' 
address2 = 'McCarthy Way 6A'
address3 = 'McCarthy Way 6AAAA'

print(re.search("(\d+\w*)", address).group())
print(re.search("(\d+\w*)", address2).group()) 
print(re.search("(\d+\w*)", address3).group())

输出:

6A
6A
6AAAA
于 2018-05-02T08:18:05.457 回答