Friday, October 1, 2010

Re: UnicodeEncodeError

On 30/09/2010 18:47, jean polo wrote:
> ok, thanks to everybody for the help but unfortunately nothing works
> for my issue.
> (except Karen one that solves it on one of my local machines but not
> the other which has the same linux system... weird..).
>
> I guess I'll ask my client to rename their files without any special
> char....
> not the best solution but a good habit anyway =)
>
> cheers,
> _y
>
>
> On Sep 30, 2:20 pm, Karen Tracey <kmtra...@gmail.com> wrote:
>> On Wed, Sep 29, 2010 at 12:59 PM, jean polo <josiano....@googlemail.com>wrote:
>>
>>
>>
>>> Hi.
>>> I get an 'UnicodeEncodeError' if I upload a file (ImageField) with non-
>>> ascii chars in my application (django-1.2.1).
>>
>>> I added:
>>
>>> export LANG='en_US.UTF-8'
>>> export LC_ALL='en_US.UTF-8'
>>
>>> in my /etc/apache2/envvars as stated here:
>>
>>> http://docs.djangoproject.com/en/dev/howto/deployment/modpython/#if-y...
>>
>>> but I still have the same error (after restarting apache).
>>> Any hint much appreciated.
>>
>> Some servers do not have the necessary language files to allow successfully
>> setting the locale to one that supports utf-8 encoding. See the very last
>> sentence here:http://code.djangoproject.com/wiki/ExpectedTestFailures
>>
>> You should be able to experiment with setting these variables in a shell
>> session and passing unicode strings containing non-ASCII characters to file
>> system routines like stat. If it works in a shell, then likely you've got
>> the necessary language support installed, and the problem then is that the
>> Apache configuration for some reason is not taking effect. If you cannot get
>> it to work in a shell either, then likely you are missing a language pack
>> that would allow successfully setting locale in this way.
>>
>> Karen
>> --http://tracey.org/kmt/
>

I had a similar problem in a python script. Not necessary to rename files :)
The problem is that when you print a debug statement or write to a file, you
need to specify the correct encoding.
I'll add a snippet of the logger class that i'm using so you'll have an idea.
Also, play with the shell and see if you can reproduce and solve the problem there.

try:
# If the message is unicode, convert to bytecode
screen_message = message
file_message = message

if ( isunicode(message) ):
screen_message = message.encode("cp850")
file_message = (message + '\n').encode("utf-8")

# Print to screen
if ( to_screen == 1 ): print screen_message

# Write to file
f = open(logfile, 'a')
f.write(file_message)
f.close()
except Exception, e:
print "Logmessage: exception %s" % str(e)

I seem to remember that on my windows, the code page in the command screen (cp850)
is different from the code page used in a file (latin1).

Anyway, as you can see, before i print a message to the cmd screen, i encode it.
Same happens when i save the message in the logfile. I encode it to a different
code page however.
You'll have to do the same with other strings that get printed.

This works for me

Regards,
Benedict

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment