mypy

Workaround for Python bug: 'ascii' codec can't encode character u'\xa0' in position 111: ordinal not in range(128)

mypy | 18 July, 2012 22:46

Have you ever needed to read unicode data file from Python?

If so, you know that it is harder than it sounds.

Even if you set your environment (e.g. export LANG=fr_FR.UTF8) to use utf-8 Python as of 2.7.1 still might not pick up this and will try to read file in ascii resulting in all too common: 'ascii' codec can't encode character u'\xa0' in position 111: ordinal not in range(128)

After lots of trial and error I found a workaround that works. First of all check if you have this problem by executing:

1
2
import sys
sys.getdefaultencoding()

if it comes back with 'ascii' then read on.

Default encoding need to be changed. However this is only possible when sys module is reloaded.

Here is a complete solution:

1
2
3
import sys;
reload(sys);
sys.setdefaultencoding("utf8")

Comments

python bug

Dave | 29/07/2012, 10:03

This is exactly the problem I had. Thanks a lot for posting this solution. Is there a Python version in which this is fixed?

Thanks

Jim K. | 17/10/2013, 21:46

Thanks dude! You saved my day!

Re: Workaround for Python bug: 'ascii' codec can't encode character u'\xa0' in position 111: ordinal not in range(128)

Emily | 25/10/2013, 01:07

After an hour of debugging finally solution is found online! Yey!

Re: Workaround for Python bug: 'ascii' codec can't encode character u'\xa0' in position 111: ordinal not in range(128)

Jacob | 28/01/2014, 10:18

I have been searching and head scratching for a day and a half before I came by this solution. It worked like a charm on the django system I'm developing. Many thanks!

Re: Workaround for Python bug: 'ascii' codec can't encode character u'\xa0' in position 111: ordinal not in range(128)

Matjaz M. | 12/02/2014, 22:47

Your solution solved the problem. Thanks!

Re: Workaround for Python bug: 'ascii' codec can't encode character u'\xa0' in position 111: ordinal not in range(128)

Carlos | 28/04/2014, 09:58

Genius! Thanks a lot!

Re: Workaround for Python bug: 'ascii' codec can't encode character u'\xa0' in position 111: ordinal not in range(128)

bach | 04/05/2014, 21:58

Finally! been looking for ages....

Re: Workaround for Python bug: 'ascii' codec can't encode character u'\xa0' in position 111: ordinal not in range(128)

Pitambar | 16/05/2014, 07:47

Superb !! thanx a lot..

Re: Workaround for Python bug: 'ascii' codec can't encode character u'\xa0' in position 111: ordinal not in range(128)

Wellngton Torrejais da Silva | 28/08/2014, 03:16

Thanks..

Re: Workaround for Python bug: 'ascii' codec can't encode character u'\xa0' in position 111: ordinal not in range(128)

G7 | 03/09/2014, 07:22

Graciassss

Re: Workaround for Python bug: 'ascii' codec can't encode character u'\xa0' in position 111: ordinal not in range(128)

wuy | 05/02/2015, 13:34

I seriously can not believe that this is even necessary, but thank you for figuring it out. A lot of the other 'solutions' to this problem are really just directions on how to fumble around with encodings...

Re: Workaround for Python bug: 'ascii' codec can't encode character u'\xa0' in position 111: ordinal not in range(128)

jtap | 16/03/2015, 20:04

Thank you!!!

Re: Workaround for Python bug: 'ascii' codec can't encode character u'\xa0' in position 111: ordinal not in range(128)

Alex | 30/03/2015, 22:21

Thank you!

Re: Workaround for Python bug: 'ascii' codec can't encode character u'\xa0' in position 111: ordinal not in range(128)

Marco | 22/02/2016, 05:18

Great!!!... It worked like charm....
Cant help but wonder.... Why does this works??

Re: Workaround for Python bug: 'ascii' codec can't encode character u'\xa0' in position 111: ordinal not in range(128)

Knowledge_should_always_be_FREE | 02/04/2016, 22:12

Thanks man - Really works like a charm !!
Keep it up - Appreciate your help !!

Re: Workaround for Python bug: 'ascii' codec can't encode character u'\xa0' in position 111: ordinal not in range(128)

VPS | 12/09/2017, 16:37

Saved my day dude!
Thanks a lot

Re: Workaround for Python bug: 'ascii' codec can't encode character u'\xa0' in position 111: ordinal not in range(128)

VPS | 12/09/2017, 16:37

Saved my day dude!
Thanks a lot

Add comment

authimage

 
Accessible and Valid XHTML 1.0 Strict and CSS
Powered by LT - Design by BalearWeb