Python UnicodeEncodeError: 'ascii' codec can't encode characterhttps://www.saltycrane.com/blog/2008/11/python-unicodeencodeerror-ascii-codec-cant-encode-character/#comments2016-09-03T03:11:25-07:00Comment by Nitish Gupta
2016-09-03T03:11:25-07:00https://www.saltycrane.com/comments/cr/18/215/#c131023
<p>my pyscripter automatically got converted to chinese (idk how) and whenever i
type a letter it shows this error: <br />
exceptions.UnicodeEncodeError: 'ascii' codec can't encode characters in
position 0-1: ordinal not in range(128). <br />
firsttly, how to switch to english and secondly, how to solve this problem?</p>
<p>disqus:2873997002</p>
Comment by Andrew P
2014-06-04T06:28:00-07:00https://www.saltycrane.com/comments/cr/18/215/#c110722
<p>Exactly what I was looking for, thanks for the quality article!</p>
Comment by Vitor Mendes
2012-10-24T04:06:42-07:00https://www.saltycrane.com/comments/cr/18/215/#c64651
<p>i know i dont need to comment but this helped me so much i needed to thank someone somehow so thank you guys you rock!</p>
Comment by Jonatas CD
2012-07-27T11:17:20-07:00https://www.saltycrane.com/comments/cr/18/215/#c59163
<p>You just seved me!</p>
<p>thanks.</p>
<p>I've also shared on DjangoBrasil google-group.</p>
Comment by EngineeringDuniya
2012-06-27T03:03:23-07:00https://www.saltycrane.com/comments/cr/18/215/#c57496
<p>Thanks. Wonderful !</p>
Comment by Joe
2011-10-16T08:38:49-07:00https://www.saltycrane.com/comments/cr/18/215/#c39773
<p>Adding the following works for me.</p>
<p>reload(sys)</p>
<p>sys.setdefaultencoding( "latin-1" )</p>
<p>Thanks! :)</p>
Comment by Chris
2011-08-20T13:56:09-07:00https://www.saltycrane.com/comments/cr/18/215/#c36951
<p>Thanks, this fixed the error I was getting when I attempted to print a QuerySet. Apparently, my model's repr() was returning Unicode, but Django's QuerySet repr() expects Ascii, causing this very confusing error. Wrapping my models repr() output with smart_str() fixed the problem.</p>
Comment by czemiello
2011-02-18T06:22:04-08:00https://www.saltycrane.com/comments/cr/18/215/#c27230
<p>You saved my life !</p>
Comment by Rippa
2011-02-03T22:48:48-08:00https://www.saltycrane.com/comments/cr/18/215/#c26265
<p>Adding the following works for me.</p>
<p>reload(sys)</p>
<p>sys.setdefaultencoding( "latin-1" )</p>
<p>Thanks! :)</p>
Comment by Federico Capoano
2010-10-13T10:26:59-07:00https://www.saltycrane.com/comments/cr/18/215/#c17902
<p>Thanks for this info, I was just looking for this!</p>
Comment by Mark R
2010-07-20T17:06:34-07:00https://www.saltycrane.com/comments/cr/18/215/#c11965
<p>Lucas's suggestion:</p>
<p><code>reload(sys)
sys.setdefaultencoding( "latin-1" )</code></p>
<p>worked great for me on ActiveState Python 2.6.5 on my Windows box. </p>
Comment by Eliot
2010-06-01T09:51:33-07:00https://www.saltycrane.com/comments/cr/18/215/#c10028
<p>Carlo: Very nice. I see that <a href="http://docs.python.org/library/stdtypes.html#str.format">format()</a> is the preferred method for formatting strings going forward in 3.0. Thanks for the tip!</p>
Comment by Carlo Pires
2010-05-31T14:55:04-07:00https://www.saltycrane.com/comments/cr/18/215/#c10001
<p>With python2.6 you can do:</p>
<pre><code>a = u'\xa1'
print format(a)
</code></pre>
Comment by Loe Spee
2010-05-05T01:04:28-07:00https://www.saltycrane.com/comments/cr/18/215/#c9322
<p>If you get this error when serializing data to JSON, it might be caused by the "ensure_ascii=False" option. Leaving this option out prevents the error form happening.</p>
<p>This will cause the error:</p>
<pre><code>serializers.serialize('json', [data], ensure_ascii=False)
</code></pre>
<p>This will prevent the error:</p>
<pre><code>serializers.serialize('json', [data])
</code></pre>
<p>More info at:</p>
<p><a href="http://groups.google.com/group/django-users/browse_thread/thread/4f5f99b730ee0aae/">http://groups.google.com/group/django-users/browse_thread/thread/4f5f99b730ee0aae/</a></p>
<p><a href="http://groups.google.com/group/django-users/browse_thread/thread/87b1478c02d743e0/">http://groups.google.com/group/django-users/browse_thread/thread/87b1478c02d743e0/</a></p>
Comment by PhilGo20
2010-04-02T12:02:23-07:00https://www.saltycrane.com/comments/cr/18/215/#c8576
<p>You saved me some time ..again. Thanks</p>
<p>I would add that if one is using DOM to output to file (dom.toxml() or dom.toprettyxml()), make sure to add "encoding="utf-8"" parameters or you will also generate the same type of errors.</p>
<pre><code>UnicodeEncodeError: 'ascii' codec can't encode character
</code></pre>
Comment by Wayle Chen
2010-03-17T21:30:11-07:00https://www.saltycrane.com/comments/cr/18/215/#c8303
<p>@Lukas: Thanks, your tips works for me.</p>
Comment by Eliot
2010-02-02T09:29:10-08:00https://www.saltycrane.com/comments/cr/18/215/#c7398
<p>chyro, Thanks for adding this information. Also, I changed your plain-text URLs into clickable links.</p>
Comment by chyro
2010-02-01T19:15:49-08:00https://www.saltycrane.com/comments/cr/18/215/#c7384
<p>Sorry about the double post, it seems I'll answer my own question. I found more information on that function here:
<a href="http://blog.ianbicking.org/illusive-setdefaultencoding.html">http://blog.ianbicking.org/illusive-setdefaultencoding.html</a>
It mostly raises the same question I did (in more details obviously). The real answers come in the second comment:
<a href="http://blog.ianbicking.org/illusive-setdefaultencoding-comment-2.html">http://blog.ianbicking.org/illusive-setdefaultencoding-comment-2.html</a>
That would kind of explain it. I'd still prefer everything being UTF-8.</p>
Comment by chyro
2010-02-01T19:06:59-08:00https://www.saltycrane.com/comments/cr/18/215/#c7383
<p>Same here, I found Lukas Monk's tip most useful as I don't want to use the "encode" function on every single string.
I'm very puzzled about the "reload(sys)" part though. Why is the "setdefaultencoding" function not present until the module is reloaded? How come it makes any difference?</p>
Comment by Tacyt
2009-12-17T02:22:09-08:00https://www.saltycrane.com/comments/cr/18/215/#c6674
<p>Thank you for this advice!</p>
Comment by Nikolai
2009-11-27T13:58:47-08:00https://www.saltycrane.com/comments/cr/18/215/#c6256
<p>saved my day! thx!</p>
Comment by Klaus
2009-11-17T04:33:58-08:00https://www.saltycrane.com/comments/cr/18/215/#c6014
<p>the tip of Lukas Monk works perfect.
As well on MS Windows.</p>
Comment by Eliot
2009-11-10T20:56:54-08:00https://www.saltycrane.com/comments/cr/18/215/#c5857
<p>William, <br />
It seems like <code>sys.setdefaultencoding</code> is not designed for us to use. From the <a href="http://docs.python.org/library/sys.html#sys.setdefaultencoding">sys module documentaton</a>:</p>
<blockquote>
<p>This function is only intended to be used by the site module implementation and, where needed, by sitecustomize. Once used by the site module, it is removed from the sys module’s namespace.</p>
</blockquote>
<p>If you're not using Django, using the <a href="http://docs.python.org/library/stdtypes.html#str.encode">encode</a> string method (described by Arthur and enoola above) seems good. From the <a href="http://code.djangoproject.com/browser/django/tags/releases/1.1.1/django/utils/encoding.py#L95">Django source code for smart_str</a>, it looks like <code>smart_str</code> uses <code>encode</code> with some other logic that you may or may not need.</p>
Comment by William
2009-11-10T20:20:32-08:00https://www.saltycrane.com/comments/cr/18/215/#c5853
<p>Python for windows do not have the attribute setdefaultencoding: What can I do to display utf-8 characters in text mode?</p>
Comment by Steve
2009-11-02T03:21:48-08:00https://www.saltycrane.com/comments/cr/18/215/#c5582
<p>Cheers - really handy!</p>
Comment by Gregory Saxton
2009-09-06T08:38:42-07:00https://www.saltycrane.com/comments/cr/18/215/#c4421
<p>Thanks--exactly what I needed. </p>
Comment by Barbara
2009-05-27T16:36:21-07:00https://www.saltycrane.com/comments/cr/18/215/#c2591
<p>Thanks so much for this - it's exactly what I needed, at exactly the right moment. :)</p>
Comment by Low Kian Seong
2009-03-19T09:57:38-07:00https://www.saltycrane.com/comments/cr/18/215/#c1294
<p>Thanks for this man. Now my edit page does not bomb out anymore. </p>
Comment by Lukas Monk
2009-02-27T02:36:40-08:00https://www.saltycrane.com/comments/cr/18/215/#c1130
<p>I use this in my main unit :</p>
<pre><code>reload(sys)
sys.setdefaultencoding( "latin-1" )
a = u'\xa1'
print str(a) # no exception
</code></pre>
Comment by enoola
2009-02-07T12:56:08-08:00https://www.saltycrane.com/comments/cr/18/215/#c1023
<p>Hi mates, I wanted to print a string with
chinese I found that simple and usefull :
cf : http://members.shaw.ca/akochoi-old/blog/2005/10-02/index.html</p>
<pre><code># Simple unicode string
y = unicode(' 麻 婆 豆 腐', 'utf-8')
# Problem with this simple call..
# print y
# UnicodeEncodeError: 'ascii' codec can't encode character u'\u9ebb' in position 1: ordinal not in range(128)
# Solution
print y.encode('utf8')
</code></pre>
Comment by Eliot
2008-11-21T09:08:21-08:00https://www.saltycrane.com/comments/cr/18/215/#c446
<p>Arthur, thanks for the tip. I'm not sure what differences the Django utility functions have. I will have to look into this further. For other readers, here is the documentation for <code>encode</code>: <a href="http://docs.python.org/library/stdtypes.html#str.encode">http://docs.python.org/library/stdtypes.html#str.encode</a></p>
Comment by Arthur Buliva
2008-11-20T09:19:49-08:00https://www.saltycrane.com/comments/cr/18/215/#c443
<p>A simpler way to do this is:</p>
<p>print unicode(u'\xa1').encode("utf-8")</p>