Python - 美国ZipCode匹配

时间:2015-04-30 14:09:10

标签: python regex

我正在与Regex合作,而且我是使用python的新手。我无法让程序从文件中读取并正确完成匹配案例。我收到的回溯错误如下:

Traceback (most recent call last):
    File "C:\Users\Systematic\workspace\Project8\src\zipcode.py", line 18, in <module>
      m = re.match(info, pattern)
    File "C:\Python34\lib\re.py", line 160, in match
      return _compile(pattern, flags).match(string)
    File "C:\Python34\lib\re.py", line 282, in _compile
      p, loc = _cache[type(pattern), pattern, flags]
TypeError: unhashable type: 'list'

zipin.txt:

3285
32816
32816-2362
32765-a234
32765-23
99999-9999

zipcode.py:

from pip._vendor.distlib.compat import raw_input
import re

userinput = raw_input('Please enter the name of the file containing the input zipcodes: ')

myfile = open(userinput)

info = myfile.readlines()

pattern = '^[0-9]{5}(?:-[0-9]{4})?$'

m = re.match(info, pattern)

if m is not None:
    print("Match found - valid U.S. zipcode: " , info, "\n")
else: print("Error - no match - invalid U.S. zipcode: ", info, "\n")

myfile.close()

1 个答案:

答案 0 :(得分:1)

问题是readlines()返回一个列表,然后对类似字符串的东西进行操作。这是一种可行的方式:

import re

zip_re = re.compile('^[0-9]{5}(?:-[0-9]{4})?$')

for l in open('zipin.txt', 'r'):
    m = zip_re.match(l.strip())
    if m:
        print l
        break
if m is None:
    print("Error - no match")

代码现在在文件行上循环操作,并尝试匹配每行的剥离版本上的re。

修改

实际上可以用更简短的方式写出来,但不太清楚:

next((l for l in open('zipin.txt', 'r') if zip_re.match(l.strip())), None)