首先,通过将所有空格折叠为每个单词之间的单个空格来简化地址字符串,并强制所有内容都小写(如果您愿意,也可以大写):
adr = " ".join(adr.tolower().split())
然后,我会删除“41街”中的“st”或“42街”中的“nd”之类的内容:
adr = re.sub("1st(\b|$)", r'1', adr)
adr = re.sub("([2-9])\s?nd(\b|$)", r'\1', adr)
请注意,第二个 sub() 将使用“2”和“nd”之间的空格,但我没有设置第一个来执行此操作;因为我不确定你如何区分“41 St Ave”和“41 St”(第二个是“41 Street”的缩写)。
请务必阅读 re 模块的所有帮助;它很强大但很神秘。
然后,我会将您剩下的内容拆分为单词列表,并应用 Soundex 算法来列出看起来不像数字的项目:
http://en.wikipedia.org/wiki/Soundexhttp://en.wikipedia.org/wiki/Soundex http://en.wikipedia.org/wiki/Soundex
http://wwwhomes.uni-bielefeld.de/gibbon/Forms/Python/SEARCH/soundex.htmlhttp://wwwhomes.uni-bielefeld.de/gibbon/Forms/Python/SEARCH/soundex.html http://wwwhomes.uni-bielefeld.de/gibbon/Forms/Python/SEARCH/soundex.html
adrlist = [word if word.isdigit() else soundex(word) for word in adr.split()]
然后,您可以使用该列表或按照您认为最好的方式将其连接回字符串。
Soundex 的整个想法是处理拼写错误的地址。这可能不是您想要的,在这种情况下,请忽略此 Soundex 想法。
祝你好运。