473,883 Members | 1,744 Online
Bytes | Software Development & Data Engineering Community
+ 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().tup les() # call to ZSQLMethod
converted = []
for result in results:
result = list(result) # make a list from the tuple
for i in range(len(resul t)):
# 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.appen d(tuple(result) ) # tuplify again
return converted

Where module_retrieve r is a simple Z SQL method that returns a module title
and a module code.
So results = context.module_ retriever().tup les() returns:
[('Developmental Neurobiology', 'ANAT2008'), ('Neuroanatomy' ,
'ANAT2009'),('e tc...', '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.CheersJo n

Jul 27 '06 #1
5 20031

Jon Bowlas wrote:
Here's my script:

results = context.module_ retriever().tup les() # call to ZSQLMethod
converted = []
for result in results:
result = list(result) # make a list from the tuple
for i in range(len(resul t)):
# 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.appen d(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*******@YOUR thirdtower.com. imagination>
Newsgroups: comp.lang.pytho n
To: <py*********@py thon.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().tup les() # call to ZSQLMethod
converted = []
for result in results:
result = list(result) # make a list from the tuple
for i in range(len(resul t)):
# 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.appen d(tuple(result) ) # tuplify again
return converted

Where module_retrieve r is a simple Z SQL method that returns a module
title and a module code.
So results = context.module_ retriever().tup les() returns:
[('Developmental Neurobiology', 'ANAT2008'), ('Neuroanatomy' ,
'ANAT2009'),('e tc...', '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.CheersJo n
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.appen d(tuple(from_la tin(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.pytho n
To: <py*********@py thon.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().tup les() # call to ZSQLMethod
converted = []
for result in results:
result = list(result) # make a list from the tuple
for i in range(len(resul t)):
# 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.appen d(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.pytho n
To: <py*********@py thon.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().tup les() # call to ZSQLMethod
converted = []
for result in results:
result = list(result) # make a list from the tuple
for i in range(len(resul t)):
# 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.appen d(tuple(result) ) # tuplify again
return converted

Where module_retrieve r is a simple Z SQL method that returns a module
title and a module code.
So results = context.module_ retriever().tup les() 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.CheersJo n

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.appen d(tuple(from_la tin(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
11232
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 = FileContents.replace('Repl_learner',unicode(Request("learner"),'utf-8','igno re')) TypeError: coercing to Unicode: need string or buffer, instance found FileContents is defined as:
8
3671
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 representation, some systems add at the beginning of the file a BOM mark (Windows?), some don't. (Linux?). Therefore, the exact same text encoded in the same UTF-8 will result in two different binary files, and of a slightly different length. Right ?
5
6586
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. I want to make convert between Unicode to string and string to Unicode.
1
1783
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
5422
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 metal bands) When this happens it throws the program as they are outside the ascii range, the program crashes as soon as I try to bind the tag to a string using the str() function. I have tried using the encode() function to change the values to...
2
3477
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 parse the file with minidom and a field from it to another variable as shown with this line: IPAddr = self.SocketSettingsObj.IPAddress I get this error:
0
1660
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 Product: Python Versions: 2.2, 2.3, 2.4 prior to 2.4.4, wide unicode (UCS-4) builds only CVE Names: CAN-2006-4980
2
6035
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 always contains single-byte coded labels (ASCII) even though I use SendMessageW entry point. In other words, buffer is unicode string each character of which contains two ASCII letters of corresponding label.
5
1990
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 with #ifndef _UNICDODE is executed in output.c, the library code for supporting printf functions. I need to how to get the code that is defined with _UNICODE to be executed instead. I have defined the _UNICODE constant in the project Properties and...
0
9938
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9791
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11140
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10847
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9572
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5797
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5991
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4611
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 we have to send another system
2
4218
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.