字符串中的街道地址搜索 - Python 或 Ruby
嘿, 我想知道如何在 Python/Ruby 中的字符串中找到街道地址?
也许通过正则表达式?
另外,它会采用以下格式(美国)
420 Fanboy Lane, Cupertino CA
谢谢!
Hey,
I was wondering how I can find a Street Address in a string in Python/Ruby?
Perhaps by a regex?
Also, it's gonna be in the following format (US)
420 Fanboy Lane, Cupertino CA
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
也许您想看看 pypostal。 pypostal 是 libpostal 的官方 Python 绑定。
根据 Mike Bethany 的示例,我制作了这个小示例:
Maybe you want to have a look at pypostal. pypostal are the official Python bindings to libpostal.
With the Examples from Mike Bethany i made this little Example:
使用您的示例,这就是我在 Ruby 中提出的内容(我对其进行了编辑以包含邮政编码和可选的 +4 邮政编码):
Using your example this is what I came up with in Ruby (I edited it to include ZIP code and an optional +4 ZIP):
这是我使用的:
它并不完美,也不匹配边缘情况,但它适用于大多数常规输入的地址和部分地址。
它在文本中查找地址,例如
希望这对某人有帮助
Here's what I used:
It's not perfect and doesn't match edge cases but it works for most regularly typed addresses and partial addresses.
It finds addresses in text such as
Hope this helps someone
尚未完全测试,但应该可以工作。只需将其与
re
中您最喜欢的函数一起使用即可(例如re.findall
)。假设:Not fully tested, but should work. Just use it with your favorite function from
re
(e.g.re.findall
. Assumptions:好的,基于 Mike Bethany 和 Rafe Kettler 非常有帮助的回复(谢谢!)
我知道这个 REGEX 适用于 python 和 ruby。
/[0-9]{1,4} (.), (.) [a-zA-Z]{2} [0-9]{5}/
Ruby 代码 -结果位于 12 Argonaut Lane, Lexington MA 02478
Python 代码 - 工作方式不太一样,但这是基本代码。
Okay, Based on the very helpful Mike Bethany and Rafe Kettler responses ( thanks!)
I get this REGEX works for python and ruby.
/[0-9]{1,4} (.), (.) [a-zA-Z]{2} [0-9]{5}/
Ruby Code - Results in 12 Argonaut Lane, Lexington MA 02478
Python Code - doesnt work quite the same, but this is the base code.
如前所述,地址的格式非常自由。与 REGEX 方法相比,提供准确、标准化地址数据的服务怎么样?我在 SmartyStreets 工作,我们提供了一个 API 来完成这件事。只需一个简单的 GET 请求,您就可以解析您的地址。尝试这个 python 示例(您需要开始试用):
https://github.com/smartystreets/smartystreets-python-sdk/blob/主/示例/us_street_single_address_example.py
As stated, addresses are very free-form. Rather than the REGEX approach how about a service that provides accurate, standardized address data? I work for SmartyStreets, where we provide an API that does this very thing. One simple GET request and you've got your address parsed for you. Try this python sample out (you'll need to start a trial):
https://github.com/smartystreets/smartystreets-python-sdk/blob/master/examples/us_street_single_address_example.py