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

Randolph Stephen Pleyto
Randolph Stephen Pleyto
7,832 Points

Can't encode character when printing data read from names.txt

Hi!

I followed the steps in the video and tried to read the data inside names.txt but when I try to print the contents of the data variable, it gives me the following Traceback error:

"UnicodeEncodeError: 'ascii' codec can't encode character '\xd6' in position 214: ordinal not in range(128)"

I saw some people solve this using the django library but I'm not there, yet. Are there other ways to resolve this error?

Here's my code:

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

Contents of the names_file.txt is the same as the one used in the video.

Many thanks!

[MOD: added ```python formatting -cf]

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

It appears to work in Python 3. Are you using Python 2?

>>> names_file = open('names.txt', encoding="utf-8")
>>> data = names_file.read()
>>> names_file.close()
>>> print(data)
Love, Kenneth   kenneth@teamtreehouse.com       (555) 555-5555  Teacher, Treehouse      @kennethlove
McFarland, Dave dave@teamtreehouse.com  (555) 555-5554  Teacher, Treehouse
Arthur, King    king_arthur@camelot.co.uk               King, Camelot
ร–sterberg, Sven-Erik    governor@norrbotten.co.se               Governor, Norrbotten    @sverik
, Tim   tim@killerrabbit.com            Enchanter, Killer Rabbit Cave
Carson, Ryan    ryan@teamtreehouse.com  (555) 555-5543  CEO, Treehouse  @ryancarson
Doctor, The     doctor+companion@tardis.co.uk           Time Lord, Gallifrey
Exampleson, Example     me@example.com  555-555-5552    Example, Example Co.    @example
Obama, Barack   president.44@us.gov     555 555-5551    President, United States of America     @potus44
Chalkley, Andrew        andrew@teamtreehouse.com        (555) 555-5553  Teacher, Treehouse      @chalkers
Vader, Darth    darth-vader@empire.gov  (555) 555-4444  Sith Lord, Galactic Empire      @darthvader
Fernรกndez de la Vega Sanz, Marรญa Teresa mtfvs@spain.gov         First Deputy Prime Minister, Spanish Govt.
>>>

1 Answer

Randolph Stephen Pleyto
Randolph Stephen Pleyto
7,832 Points

Update:

Tried running it again in Terminal and it works fine. I was using VS Code with the Python and Code Runner plugin installed. There must be something in the Code Runner that can't encode the character.

Workaround: run the code in terminal instead.