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 trialSophia Zeng
2,169 PointsAttributeError: 'list' object has no attribute 'groupdict'
Hi,
I think I followed exactly everything in Kenneth's video, but when I run it, I got an attribute error like this: "AttributeError: 'list' object has no attribute 'groupdict'"... I don't know what's wrong with my code, can someone help me please?!
import re
names_file = open('names.txt', encoding="utf8")
data = names_file.read()
names_file.close()
line = re.findall(r'''
^(?P<name>[-\w ]*, \s[-\w ]+)\t # Last and first names
(?P<email>[-\wd.+]+@[-\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())
[MOD: added ```python markdown formatting -cf]
1 Answer
Chris Freeman
Treehouse Moderator 68,441 PointsThe re.findall()
method is different from using re.match()
. findall()
returns a list
, where match()
returns a match object
. A match object
has the methods: 'end', 'endpos', 'expand', 'group', 'groupdict', 'groups', 'lastgroup', 'lastindex', 'pos', 're', 'regs', 'span', 'start', 'string'
From help(re.findall)
findall(pattern, string, flags=0)
Return a list of all non-overlapping matches in the string.
If one or more capturing groups are present in the pattern, return
a list of groups; this will be a list of tuples if the pattern
has more than one group.
Empty matches are included in the result.
Sophia Zeng
2,169 PointsSophia Zeng
2,169 PointsI get it!! Just realised I wasn't using re.search!! Thanks a lot Chris Freeman !!