Right solution to unicode error?

Oscar Benjamin oscar.j.benjamin at gmail.com
Wed Nov 7 18:27:17 EST 2012


On 7 November 2012 22:17, Anders <aschneiderman at asha.org> wrote:
>
> Traceback (most recent call last):
>   File "outlook_tasks.py", line 66, in <module>
>     my_tasks.dump_today_tasks()
>   File "C:\Users\Anders\code\Task List\tasks.py", line 29, in
> dump_today_tasks
>     print task.subject
> UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in
> position 42: ordinal not in range(128)
>
> Here's where I'm getting stuck.  In the code above I was just printing
> the subject so I can see whether the script is working properly.
> Ultimately what I want to do is parse the tasks I'm interested in and
> then create an HTML file containing those tasks.  Given that, what's
> the best way to fix this problem?

Are you using cmd.exe (standard Windows terminal)? If so, it does not
support unicode and Python is telling you that it cannot encode the
string in a way that can be understood by your terminal. You can try
using chcp to set the code page to something that works with your
script.

If you are only printing it for debugging purposes you can just print
the repr() of the string which will be ascii and will come out fine in
your terminal. If you want to write it to a html file you should
encode the string with whatever encoding (probably utf-8) you use in
the html file. If you really just want your script to be able to print
unicode characters then you need to use something other than cmd.exe
(such as IDLE).


Oscar



More information about the Python-list mailing list