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 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
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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 =...
|
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...
|
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....
|
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
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
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...
| | |