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 trialRandolph Stephen Pleyto
7,832 PointsCan'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]
1 Answer
Randolph Stephen Pleyto
7,832 PointsUpdate:
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.
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsIt appears to work in Python 3. Are you using Python 2?