I am trying to use Regex to return Canadian postal codes through a each line of a CSV file.
Environment: Python 3.6 on Win 10. Code tested through Jupyter Notebook and through the Win 10 CLI prompt.
The problem is that I can't seem to get the object to return the string when found using a FOR LOOP through a CSV file.
Using re through a list works fine:
import re
address = [ 'H1T3R9',
'/a/b/c/la_seg_x005_y003.npy',
'H1K 3H3',
'F2R2V2',
'H1L 3W6',
'j1r 4v5',
'/y',
'h2r 2x8',
'J9R 5V9',
'Non disponible, h2r 2x8, montreal']
# I also tried this one at some point,# r'^((\d{5}-\d{4})|(\d{5})|([AaBbCcEeGgHhJjKkLlMmNnPpRrSsTtVvXxYy]\d[A-Za-z]\s?\d[A-Za-z]\d))$))
regex = re.compile(r'\b[a-z]\d[a-z]\s\d[a-z]\d\b')
goodPostalCode = filter(regex.search, address)
print(*goodPostalCode)
Output:
j1r 4v5 h2r 2x8 Non disponible, h2r 2x8, montreal
But when adding the CSV component it seems to break.
import re
import csv
with open('data.csv', newline='') as f:
reader = csv.reader(f)
for row in reader:
#print(row)
regex = re.compile(r'\b[a-z]\d[a-z]\s\d[a-z]\d\b')
postcode = filter(regex.search, row[7])
print(postcode)
Output:
<filter object at 0x000001E4FA70D908>
The object filter object seems to be found every iteration
My understanding was that I could loop through a CSV as each line would return a list or a tuple, then I could use *re to find matching patterns in the string at a specific column using its index.
Where do I go wrong here?