469,343 Members | 5,350 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,343 developers. It's quick & easy.

coercing to Unicode: need string or buffer, NoneType found

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[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

Jul 27 '06 #1
5 19675

Jon Bowlas wrote:
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[i] is None: continue
result[i] = unicode(result[i], 'latin-1')
converted.append(tuple(result)) # tuplify again
return converted
regards,
Rob

Jul 27 '06 #2
It says line 8 in the traceback so I guess its

result[i] = unicode(result[i], 'latin-1')

Jon
----- Original Message -----
From: "Sybren Stuvel" <sy*******@YOURthirdtower.com.imagination>
Newsgroups: comp.lang.python
To: <py*********@python.org>
Sent: Thursday, July 27, 2006 11:06 AM
Subject: Re: coercing to Unicode: need string or buffer, NoneType found

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
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
--
http://mail.python.org/mailman/listinfo/python-list
Jul 27 '06 #3
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

Jul 27 '06 #4
Ahh, that did it, thanks very much.

Jon
----- Original Message -----
From: <bl****@interia.pl>
Newsgroups: comp.lang.python
To: <py*********@python.org>
Sent: Thursday, July 27, 2006 11:19 AM
Subject: Re: coercing to Unicode: need string or buffer, NoneType found

>
Jon Bowlas wrote:
>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[i] is None: continue
> result[i] = unicode(result[i], 'latin-1')
converted.append(tuple(result)) # tuplify again
return converted

regards,
Rob

--
http://mail.python.org/mailman/listinfo/python-list
Jul 27 '06 #5
Ahh yes there are a couple of dodgy records that seem to have been added.

many thanks.

Jon
----- Original Message -----
From: "Peter Otten" <__*******@web.de>
Newsgroups: comp.lang.python
To: <py*********@python.org>
Sent: Thursday, July 27, 2006 11:22 AM
Subject: Re: coercing to Unicode: need string or buffer, NoneType found

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

--
http://mail.python.org/mailman/listinfo/python-list
Jul 27 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Francis Girard | last post: by
5 posts views Thread by [Yosi] | last post: by
1 post views Thread by Scott Duckworth | last post: by
1 post views Thread by John Morey | last post: by
2 posts views Thread by =?Utf-8?B?QWxleCBLLg==?= | last post: by
5 posts views Thread by =?Utf-8?B?Q3JhaWcgSm9obnN0b24=?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.