Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Regular Expressions in Python Introduction to Regular Expressions Groups

Getting no return value when doing the search

I swear I have copied it letter for letter as in the video. When running this in workspaces there are two different issues that I receive.

Here is a copy of the code I used.

import re

names_file = open("names.txt", encoding="utf-8")
data = names_file.read()
names_file.close()


line = re.search(r'''
  ^(?P<name>[-\w ]*,\s[-\w ]+) # first and last name
  (?P<email>]-\w\d.+]+@[-\w\d.]+)\t # email
  (?P<phone>\(?\d{3}\)?-?\s?\d{3}-\d{4})?\t # phone
  (?P<job>[\w\s]+,\s[\w\s.]+)\t? # job and company
  (?P<twitter>@[\w\d]+)?$ #Twitter
''', data, re.X| re.M)

print(line)
print(line.groupdict())

When the print(line.groupdict()) is not commented out. I receive an error stating:

AttributeError: 'None type' object has no attribute 'groupdict'

With that line commented out I receive the value of "None" in which in the video has a value returned.

Can someone please tell me what I am doing wrong.

Thank you.

2 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Look at your < email> line. Your first set starts with ] instead of [. Also, you probably don't want the space between the pipe | and re.M.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

There may be some other problems, but I don't see any from a quick glance.

Also you missed a \t at the end of the < name> line

Rabih Atallah
Rabih Atallah
3,405 Points

I think Jeremy is right on the : print(line.groupdict()) it returns AttributeError

I presume line doesn't have .groupdict() attribute because it's not a dictionary, and it's not a dictionary because the match failed, and the match failed because of the missing \t

but I may be wrong, try the \t at the end of the < name> line and see