On 14 Jan 2011, at 01:31, Alex Grönholm wrote:
> 14.01.2011 02:24, claudef@... kirjoitti:
>>
>> Dear Alex and Marcel,
>>
>> Excuse me to ask. Why it won't work ? Observing the error message, I see a clear sign the the character delivered by the query is above the 7 bit boundary. See the error message:
>>
>> UnicodeEncodeError: 'ascii' codec can't encode character u'\xe1' in position 11: ordinal not in range(128)
>>
>> Character "á" = x'E1' Dec 225 is a typical Latin Character
>>
>> The encoding "Latin1" is a common 8 bit extended ASCII representation which should accept the 8 bit characters delivered by the query,
>> So the statement sys.setdefaultencoding("latin1") has a good chance to work O.K.
>>
>> However you are able to translate a string regardless of its current encoding used by Jython, by adding the function encode("Latin1), or encode("UTF-8") if you prefer so. This is another solution that should work in the Jython release 2.5.x.
>>
> This is because the "sys" module does not HAVE a "setdefaultencoding" function during runtime!
> Let me paste you the documentation from python.org:
>
> setdefaultencoding(name)
> Set the current default string encoding used by the Unicode implementation. If name does not match any available encoding, LookupError is raised. 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. New in version 2.0.
Thanks guys for all your good suggestions.
I have been able to workaround the encoding issue as follows:
>>> import sys
>>> reload(sys)
<module 'sys' (built-in)>
>>> sys.setdefaultencoding("UTF-8")
>>> db.isql("select name from table where condition")
NAME
---------
Ignácio
I found the solution here http://blog.ianbicking.org/illusive-setdefaultencoding.html
However, I don't believe that this workaround should be necessary.
My $LANG variable in shell is:
> echo $LANG
en_US.UTF-8
Is there a way to hint the jython startup script to set UTF-8 as default encoding?
>> What I can say, give it a trial.
>>
>> Regards,
>> Claude
>>
>> Claude Falbriard
>> Certified IT Specialist L2 - Middleware
>> AMS Hortolândia / SP - Brazil
>> phone: +55 13 9762 4094
>> cell: +55 13 8117 3316
>> e-mail: claudef@...
>>
>>
>>
>>
>> From: Alex Grönholm <alex.gronholm@...>
>> To: jython-users@...
>> Date: 13/01/2011 21:12
>> Subject: Re: [Jython-users] dbexts UnicodeEncodeError
>>
>>
>>
>> 13.01.2011 17:38, claudef@... kirjoitti:
>> Dear Marcel,
>>
>> give this a trial with:
>>
>> sys.setdefaultencoding("latin1")
>> This one won't work.
>>
>> or
>> mystr = mystr.encode('latin1')
>>
>> Regards,
>> Claude
>>
>> Claude Falbriard
>> Certified IT Specialist L2 - Middleware
>> AMS Hortolândia / SP - Brazil
>> phone: +55 13 9762 4094
>> cell: +55 13 8117 3316
>> e-mail: claudef@...
>>
>>
>>
>>
>> From: marcel liker <m_liker@...>
>> To: jython-users <jython-users@...>
>> Date: 13/01/2011 12:27
>> Subject: [Jython-users] dbexts UnicodeEncodeError
>>
>>
>>
>> Hi all,
>> is the below example expected behavior of dbexts console formater?
>>
>> >>> db.isql("select * from table where condition)
>>
>> Traceback (most recent call last):
>> File "<stdin>", line 1, in <module>
>> File "/home/user/jython252rc2/Lib/dbexts.py", line 332, in isql
>> self.display()
>> File "/home/user/jython252rc2/Lib/dbexts.py", line 312, in display
>> for a in self.formatter(res, map(lambda x: x[0], self.headers)):
>> File "/home/user/jython252rc2/Lib/dbexts.py", line 85, in console
>> row = map(lambda x: str(x), row)
>> File "/home/user/jython252rc2/Lib/dbexts.py", line 85, in <lambda>
>> row = map(lambda x: str(x), row)
>> UnicodeEncodeError: 'ascii' codec can't encode character u'\xe1' in position 11: ordinal not in range(128)
>>
>> While:
>>
>> >>> print db.results[0][2]
>> Ignácio
>>
>> And from the above trace:
>>
>> >>> str(prod.results[0][2])
>> Traceback (most recent call last):
>> File "<stdin>", line 1, in <module>
>> UnicodeEncodeError: 'ascii' codec can't encode character u'\xe1' in position 11: ordinal not in range(128)
>>
>> I also tested this with 2.5.2rc3 and the behavior is the same.
>>
>> Tested on sles 10 with java1.6.0_14
>>
>>
>
> ------------------------------------------------------------------------------
> Protect Your Site and Customers from Malware Attacks
> Learn about various malware tactics and how to avoid them. Understand
> malware threats, the impact they can have on your business, and how you
> can protect your company and customers by using code signing.
> http://p.sf.net/sfu/oracle-sfdevnl_______________________________________________
> Jython-users mailing list
> Jython-users@...
> https://lists.sourceforge.net/lists/listinfo/jython-users
|