Right solution to unicode error?
ramit.prasad at jpmorgan.com
Thu Nov 8 00:07:33 CET 2012
> I've run into a Unicode error, and despite doing some googling, I
> can't figure out the right way to fix it. I have a Python 2.6 script
> that reads my Outlook 2010 task list. I'm able to read the tasks from
> Outlook and store them as a list of objects without a hitch. But when
> I try to print the tasks' subjects, one of the tasks is generating an
> Traceback (most recent call last):
> File "outlook_tasks.py", line 66, in <module>
> File "C:\Users\Anders\code\Task List\tasks.py", line 29, in
> print task.subject
> UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in
> position 42: ordinal not in range(128)
> (where task.subject was previously assigned the value of
> task.Subject, aka the Subject property of an Outlook 2010 TaskItem)
> From what I understand from reading online, the error is telling me
> that the subject line contains an en dash and that Python is trying
> to convert to ascii and failing (as it should).
> 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?
> BTW, if there's a clear description of the best solution for this
> particular problem - i.e., where I want to ultimately display the
> results as HTML - please feel free to refer me to the link. I tried
> reading a number of docs on the web but still feel pretty lost.
You can always encode in a non-ASCII codec.
`print task.subject.encode(<encoding>)` where <encoding> is something that
supports the characters you want e.g. latin1.
The list of built in codecs can be found:
This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.
More information about the Python-list