[Zope3-Users] Unicode and vocabularies

Darryl Cousins darryl at darrylcousins.net.nz
Fri Sep 29 21:52:35 EDT 2006


Hi,

I'm having some trouble with unicode and vocabularies.

I've a utility with a List field:

class MyUtility(Interface):

	mylist = List(title=_("My List"),
                      value_type = TextLine(_("list item")),
                      default = [],
		      required = False)

When the utility is created the list is populated from a unicode text
file:

    datadir = os.path.join(os.path.dirname(__file__),'data')
    source_file = os.path.join(datadir, 'medium.dat')
    fp = codecs.open(source_file,"r",'utf-8')
    options = []
    for line in fp.readlines():
        options.append(line.strip())
    util.medium = options
    fp.close()

The file contains é (e-acute). I came to the above routine to fix a
'Wrong Type' error for the list.

Now when I come to create a vocabulary from the list:

class MyVocabulary(object):

    def __call__(self, context):
        utility = zapi.getUtility(IMyUtility)
        items = [item for item in utility.medium]
        return SimpleVocabulary.fromValues(items)

It raises error

...
File "/opt/zope/zope3/src/zope/schema/vocabulary.py", line 41, in
__init__
        self.token = str(token)
    UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in
position 13: ordinal not in range(128)with vocabulary code:

I can't figure out how to get the correct unicode value into the utility
list and yet still have str(u'é') work when vocabulary factory is
called.

Best regards,
Darryl



More information about the Zope3-users mailing list