coercing to Unicode: need string or buffer, NoneType found

Peter Otten __peter__ at web.de
Thu Jul 27 06:22:38 EDT 2006


Jon Bowlas wrote:

> I wrote this script in Zope some time ago and it worked for a while, but
> now I'm getting the following error:
> TypeError: coercing to Unicode: need string or buffer, NoneType found
> 
> Here's my script:
> 
> results = context.module_retriever().tuples() # call to ZSQLMethod
> converted = []
> for result in results:
>    result = list(result) # make a list from the tuple
>    for i in range(len(result)):
>     # for each element in the listified tuple, make decode to a
>     # unicode from the latin-1 string
>     result[i] = unicode(result[i], 'latin-1')
>    converted.append(tuple(result)) # tuplify again
> return converted
> 
> Where module_retriever is a simple Z SQL method that returns a module
> title and a module code.
> So results = context.module_retriever().tuples() returns:
> [('Developmental Neurobiology', 'ANAT2008'), ('Neuroanatomy',
> 'ANAT2009'),('etc...', 'etc..'),..]So I was hoping someone might be able
> to identify what I've done wrong here. It is a little mistifying as to why
> it's suddenly stopped working though.CheersJon

This may be an indication that in your database you have a record with a
None value, e. g.

        [('Developmental Neurobiology', 'ANAT2008'),
         ('Neuroanatomy', 'ANAT2009'),
         ('Man in black', None)])

and most likely the right fix is to correct the database entry and make sure
that no new such records can be entered into it.

If that is not possible, a quick fix would be to replace your unicode() call
with something that special-cases None:

def from_latin(s):
    if s is None:
        return None # or maybe 'return u""', if your app expects a unicode
                    # string
    return unicode(s, "latin-1")

By the way, in recent Python your snippet might become

    converted = []
    for record in results:
        # prior to 2.4: tuple([...])
        converted.append(tuple(from_latin(field) for field in record))
    return converted

Peter




More information about the Python-list mailing list