user3062459
user3062459

Reputation: 1631

AttributeError: 'NoneType' object has no attribute 'group'

I am trying to print three variables, but I am getting a AttributeError

"1 -3 JANE STREET "
"217- 219 EASTERN PARKWAY "
"219 -223 78TH STREET "
"28-31 34TH ST "

Sample data for row['address'] (above)

import csv
import re
with open('/Users/d/Desktop/bldg_zip_codes.csv', 'r+') as f:
    reader = csv.DictReader(f)
    for index, row in enumerate(reader):
        if row['address'] != None:
          addy1 = re.search(r'\d\d-\d\d', row['address'])
          addy2 = re.search(r'-\s\d\d', row['address'])
          addy3 = re.search(r'\d\s-\d', row['address'])
          print(addy1.group(), addy2.group(), addy3.group())

 Traceback (most recent call last):
 File "excludenumsonst.py", line 46, in <module>
 print(addy1.group(), addy2.group(), addy3.group())
 AttributeError: 'NoneType' object has no attribute 'group'

Why am I getting this error if I have already excluded NoneType on line 6?

How can I resolve this issue?

Upvotes: 0

Views: 1518

Answers (2)

Hamza Abbad
Hamza Abbad

Reputation: 666

You didn't exclude None simply because it is generated from re.search. Your regular expressions doesn't match what you need to extract. The first regexp matches a succession of 2 digits followed by a hyphen then followed by another 2 digits, for example "28-31". The second regexp matches a hyphen followed by space then followed by 2 digits, for example : "- 21". The last one matches a digit followed a space then followed by a hyphen then another digit, for example : "1 -3".

You have to check those regular expressions to make them match what you need.

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626758

'NoneType' object has no attribute 'group' appears when your regex has not matched anything inside the provided input string, because the match object is None, not initialized, the .group() is None.

The \d\d-\d\d pattern will only match 28-31 in 28-31 34TH ST, -\s\d\d will only match - 21 in 217- 219 EASTERN PARKWAY, and \d\s-\d will match 1 -3 and 9 -2 in 1 -3 JANE STREET and 219 -223 78TH STREET.

To match the initial digits with a hyphen, you can use

^\d+\s*-\s*\d+

See the regex demo

In code, you can use re.match to match in the beginning of a string rather than use ^ with re.search:

if row['address']:                                    # Check if address is present
    add = re.match(r'\d+\s*-\s*\d+', row['address'])  # Run the regex
    if add:                                           # Regex matched?
        print(add.group())                            # Print the match value

Upvotes: 3

Related Questions