473,698 Members | 2,602 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 20011

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
11211
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
3650
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
6573
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
1772
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
5409
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
3465
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
1645
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
6014
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
1984
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
8680
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
8609
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
9030
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8899
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
8871
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6528
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
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...
3
2007
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.