0

我试图弄清楚如何比较我从两个 XML 源中提取的两个地址字段。一个以这种格式返回地址:

123 Main St

和另外一个:

123 MAIN ST

我试着做这样的事情:

x = address1  <---first address format i states
y = address2   <---and the second

if x.upper() == y.upper():
   print "correct"
else:
   print "incorrect"

这似乎不起作用,我假设这是因为我试图将地址的整个字符串设置为大写,但这不适用于数字。我的问题是我将如何比较以不同格式返回的两个地址,如果 x = y 比较无法比较地址,则会出现海峡。

4

1 回答 1

1

您可以使用 .lower() 将两个地址转换为小写,然后进行比较。

小写或大写无关紧要,可能问题是您使用运算符“=”而不是“==”导致分配。

例如:

x = '123 Main St'
y = '123 MAIN ST'

new_x = x.lower()
new_y = y.lower()



if new_x == new_y :
   print "correct"
else:
   print "incorrect"
于 2012-11-27T16:41:22.223 回答