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
Sam Dale
6,136 PointsCan't Solve Blank List Problem
Hi there. No matter what I do I cannot figure out why the following code prints out an empty list. I'm fairly certain that it is all the exact same code as was shown. I'd love it if someone could help me find the obvious mistake that's eluding me. Thanks!
import re
names_file = open("names.txt", encoding = "utf-8")
data = names_file.read()
names_file.close()
print(re.findall(r"""
([-\w ]*,\s[-\w ]+)\t # Last and first names
([-\w\d.+]+@[-\w\d.]+)\t # Email
(\(?\d{3}\)?-?\s?\d{3}-\d{4})\t # Phone
([\w\s]+,\s[\w\s]+)\t # Job and company
(@[\w\d]+) # Twitter
""", data, re.X))
The names.txt looks like:
Love, Kenneth [email protected] (555) 555-5555 Teacher, Treehouse @kennethlove
McFarland, Dave [email protected] (555) 555-5554 Teacher, Treehouse
Arthur, King [email protected] King, Camelot
Österberg, Sven-Erik [email protected] Governor, Norrbotten @sverik
, Tim [email protected] Enchanter, Killer Rabbit Cave
Carson, Ryan [email protected] (555) 555-5543 CEO, Treehouse @ryancarson
Doctor, The [email protected] Time Lord, Gallifrey
Exampleson, Example [email protected] 555-555-5552 Example, Example Co. @example
Obama, Barack [email protected] 555 555-5551 President, United States of America @potus44
Chalkley, Andrew [email protected] (555) 555-5553 Teacher, Treehouse @chalkers
Vader, Darth [email protected] (555) 555-4444 Sith Lord, Galactic Empire @darthvader
Fernández de la Vega Sanz, Maria Teresa [email protected] First Deputy Prime Minister, Spanish Govt.
1 Answer
Chris Freeman
68,469 PointsI found these errors:
- the
names.txtdata wasn't separated by TABs, just spaces. But that might just be a markdown or cut-and-paste issue - need to add
?outside of parens for fields that are optional such as email, phone, twitter - missing . for company name
- the
reflag for MULTILINE needs to added:re,X | re.Mto parse passed the first line
# fixed version
results = re.findall(r"""
([-\w ]*,\s[-\w ]+)\t # Last and first names
# add ? for option email
([-\w\d.+]+@[-\w\d.]+)?\t # Email
# add ? for option phone
(\(?\d{3}\)?-?\s?\d{3}-\d{4})?\t # Phone
# add . for company name
([\w\s]+,\s[\w\s.]+)\t # Job and company
# add ? for option twitter
(@[\w\d]+)? # Twitter
""", data, re.M | re.X)
Chris Freeman
68,469 PointsUpdated answer with complete fixed version.
Sam Dale
6,136 PointsThank you. That helped a lot.
Afrid Mondal
6,255 PointsAfrid Mondal
6,255 Pointsmake the phone optional by adding ? at last and your tab creating the problem maybe. Try this,i think it should help..