设置
我有包含英国格式地址的字符串,例如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 的方式。
如何改进代码?