473,480 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 19983

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
11178
by: Stuart Forsyth | last post by:
What can I do about this error, i am tearing my hair out! Python ActiveX Scripting Engine (0x80020009) Traceback (most recent call last): File "<Script Block >", line 58, in ? FileContents =...
8
3639
by: Francis Girard | last post by:
Hi, For the first time in my programmer life, I have to take care of character encoding. I have a question about the BOM marks. If I understand well, into the UTF-8 unicode binary...
5
6554
by: [Yosi] | last post by:
Hi, I have a string array includes unicode data, how can I print the char (real string), for example: "\x08\x03\x34\0.\0\x39\0" What should I do, I want to see the char of this array of unicode....
1
1755
by: Scott Duckworth | last post by:
Can anyone provide a quick code snippit to open a text file and tell if it's ASCII or Unicode? Thanks
1
5378
by: John Morey | last post by:
My program reads ID3 tags from MP3 files and enters them into a database. I have been testing it on a variety of MP3s, including ones with weird characters in the tags (such as norweigan black...
2
3442
by: Randall Parker | last post by:
Running Python 2.4.2 on Windows in SPE. I have a very small XML file (see below) that is in UTF-8 and saved by Windows Notepad as such. I'm reading it with minidom. My problem is that once I...
0
1629
by: Anthony Baxter | last post by:
SECURITY ADVISORY Buffer overrun in repr() for UCS-4 encoded unicode strings http://www.python.org/news/security/PSF-2006-001/ Advisory ID: PSF-2006-001 Issue Date: October 12, 2006...
2
5988
by: =?Utf-8?B?QWxleCBLLg==?= | last post by:
Hi all My TreeView has unicode and english labels. The treeview shows OK on the screen. When I am trying to get an item's label using TVM_GETITEM API message, the buffer returned by SendMessage...
5
1957
by: =?Utf-8?B?Q3JhaWcgSm9obnN0b24=?= | last post by:
I am in the process of converting an application to Unicode that is built with Visual C++ .NET 2003. On application startup in debug mode I get an exception. The problem appears to be that code...
0
7039
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6904
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7037
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
6895
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4770
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2992
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2977
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1296
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
176
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.