coercing to Unicode: need string or buffer, NoneType found

J

Jon Bowlas

Hi listers,

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 = unicode(result, '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
 
S

Sybren Stuvel

Jon Bowlas enlightened us with:
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

What line is causing the error?

Sybren
 
B

blue99

Jon said:
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

try this:
if result is None: continue
result = unicode(result, 'latin-1')
converted.append(tuple(result)) # tuplify again
return converted


regards,
Rob
 
J

Jon Bowlas

It says line 8 in the traceback so I guess its

result = unicode(result, 'latin-1')

Jon
----- Original Message -----
From: "Sybren Stuvel" <[email protected]>
Newsgroups: comp.lang.python
To: <[email protected]>
Sent: Thursday, July 27, 2006 11:06 AM
Subject: Re: coercing to Unicode: need string or buffer, NoneType found
 
P

Peter Otten

Jon said:
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 = unicode(result, '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
 
J

Jon Bowlas

Ahh, that did it, thanks very much.

Jon
----- Original Message -----
From: <[email protected]>
Newsgroups: comp.lang.python
To: <[email protected]>
Sent: Thursday, July 27, 2006 11:19 AM
Subject: Re: coercing to Unicode: need string or buffer, NoneType found

Jon said:
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

try this:
if result is None: continue
result = unicode(result, 'latin-1')
converted.append(tuple(result)) # tuplify again
return converted


regards,
Rob
 
J

Jon Bowlas

Ahh yes there are a couple of dodgy records that seem to have been added.

many thanks.

Jon
----- Original Message -----
From: "Peter Otten" <[email protected]>
Newsgroups: comp.lang.python
To: <[email protected]>
Sent: Thursday, July 27, 2006 11:22 AM
Subject: Re: coercing to Unicode: need string or buffer, NoneType found

Jon said:
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 = unicode(result, '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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top