[Tutor] Encode problem

Pablo P. F. de Faria pablofaria at gmail.com
Mon May 4 16:09:25 CEST 2009

Thanks, Kent, but that doesn't solve my problem. In fact, I need
ConfigParser to work with non-ascii characters, since my App may run
in "latin-1" environments (folders e files names). I must find out why
the str() function in the module ConfigParser doesn't use the encoding
defined for the application (# -*- coding: utf-8 -*-). The rest of the
application works properly with utf-8, except for ConfigParser. What I
found out is that ConfigParser seems to make use of the configuration
in Site.py (which is set to 'ascii'), instead of the configuration
defined for the App (if I change . But this is very problematic to
have to change Site.py in every computer... So I wonder if there is a
way to replace the settings in Site.py only for my App.

2009/5/1 Kent Johnson <kent37 at tds.net>:
> On Fri, May 1, 2009 at 4:54 PM, Pablo P. F. de Faria
> <pablofaria at gmail.com> wrote:
>> Hi, Kent.
>> The stack trace is:
>> Traceback (most recent call last):
>>  File "/home/pablo/workspace/E-Dictor/src/MainFrame.py", line 1057, in OnClose
>>    self.SavePreferences()
>>  File "/home/pablo/workspace/E-Dictor/src/MainFrame.py", line 1068,
>> in SavePreferences
>>    self.cfg.set(u'File Settings',u'Recent files',
>> unicode(",".join(self.recent_files)))
>> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position
>> 12: ordinal not in range(128)
>> The "unicode" function, actually doesn't do any difference... The
>> content of the string being saved is "/home/pablo/Área de
>> Trabalho/teste.xml".
> OK, this error is in your code, not the ConfigParser. The problem is with
> ",".join(self.recent_files)
> Are the entries in self.recent_files unicode strings? If so, then I
> think the join is trying to convert to a string using the default
> codec. Try
> self.cfg.set('File Settings','Recent files',
> ','.join(name.encode('utf-8') for name in self.recent_files))
> Looking at the ConfigParser.write() code, it wants the values to be
> strings or convertible to strings by calling str(), so non-ascii
> unicode values will be a problem there. I would use plain strings for
> all the interaction with ConfigParser and convert to Unicode yourself.
> Kent
> PS Please Reply All to reply to the list.

"Estamos todos na sarjeta, mas alguns de nós olham para as estrelas."
(Oscar Wilde)
Pablo Faria
Mestrando em Aquisição de Linguagem - IEL/Unicamp
Bolsista técnico FAPESP no Projeto Padrões Rítmicos e Mudança Lingüística
(19) 3521-1570
pablofaria at gmail.com

More information about the Tutor mailing list