SystematicAutomatic
SystematicAutomatic

Reputation: 3

Python - U.S. ZipCode Matching

I'm working with Regex and I'm brand new to using python. I can't get the program to read from file and go through the match case properly. I'm getting a traceback error that looks like this:

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()

Upvotes: 0

Views: 1920

Answers (1)

Ami Tavory
Ami Tavory

Reputation: 76297

The problem is that readlines() returns a list, and re operates on stuff that is string like. Here is one way it could work:

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")

The code now operates in a loop over the file lines, and attempts to match the re on a stripped version of each line.

Edit:

It's actually possible to write this in a much shorter, albeit less clear way:

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

Upvotes: 1

Related Questions