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 Reading Files

Babak Damadi
PLUS
Babak Damadi
Courses Plus Student 3,133 Points

with Open("names.txt")

Not sure about this error while replacing the code with the

import re with open("names.txt") as open_file: data = open_file.read() last_name = r'Love' first_name = r'Kenneth' print(re.match(last_name, data)) # bigining of string print(re.search(first_name, data)) # somewhere in the string

File "address_book.py", line 4, in <module>
data = open_file.read()
File "/usr/lib/python3.4/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 217: ordinal not in range(128)

2 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Doing this on your computer instead of in Workspaces?

Some terminals aren't set up to display unicode characters (boo). What system are you on and, if you're on Mac or Linux, what shell (Bash, ZSH, etc) are you using?

The discussion on the following question on Stack Overflow presents two options: Why we need sys.setdefaultencoding(“utf-8”) in a py script?

  1. Add the following to the top of each script that uses Unicode (though apparently this practice is discouraged):

    import sys
    reload(sys)
    sys.setdefaultencoding("utf-8")
    
  2. If running Mac OS X or Linux, enter the following in the command line before running your script:

    export PYTHONIOENCODING=UTF-8
    

I didn't get those errors so I haven't been able to confirm either of these for this course...