coercing to Unicode: need string or buffer, NoneType found

Discussion in 'Python' started by Jon Bowlas, Jul 27, 2006.

  1. Jon Bowlas

    Jon Bowlas Guest

    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 = unicode(result, '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
    Jon Bowlas, Jul 27, 2006
    #1
    1. Advertising

  2. 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
    Sybren Stuvel, Jul 27, 2006
    #2
    1. Advertising

  3. Jon Bowlas

    Guest

    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 is None: continue

    > result = unicode(result, 'latin-1')
    > converted.append(tuple(result)) # tuplify again
    > return converted


    regards,
    Rob
    , Jul 27, 2006
    #3
  4. Jon Bowlas

    Jon Bowlas Guest

    It says line 8 in the traceback so I guess its

    result = unicode(result, 'latin-1')

    Jon
    ----- Original Message -----
    From: "Sybren Stuvel" <>
    Newsgroups: comp.lang.python
    To: <>
    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, Jul 27, 2006
    #4
  5. Jon Bowlas

    Peter Otten Guest

    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 = unicode(result, '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
    Peter Otten, Jul 27, 2006
    #5
  6. Jon Bowlas

    Jon Bowlas Guest

    Ahh, that did it, thanks very much.

    Jon
    ----- Original Message -----
    From: <>
    Newsgroups: comp.lang.python
    To: <>
    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 is None: continue
    >
    >> result = unicode(result, 'latin-1')
    >> converted.append(tuple(result)) # tuplify again
    >> return converted

    >
    > regards,
    > Rob
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    Jon Bowlas, Jul 27, 2006
    #6
  7. Jon Bowlas

    Jon Bowlas Guest

    Ahh yes there are a couple of dodgy records that seem to have been added.

    many thanks.

    Jon
    ----- Original Message -----
    From: "Peter Otten" <>
    Newsgroups: comp.lang.python
    To: <>
    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 = unicode(result, '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
    >
    Jon Bowlas, Jul 27, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stuart Forsyth
    Replies:
    1
    Views:
    4,925
    vincent wehren
    Nov 13, 2003
  2. Randall Parker
    Replies:
    2
    Views:
    814
    Fredrik Lundh
    Mar 22, 2006
  3. Max Erickson
    Replies:
    0
    Views:
    1,265
    Max Erickson
    Jun 21, 2006
  4. Fredrik Lundh
    Replies:
    0
    Views:
    773
    Fredrik Lundh
    Jun 21, 2006
  5. Jordan Harry
    Replies:
    1
    Views:
    478
    Sean DiZazzo
    May 2, 2008
Loading...

Share This Page