'ascii' codec can't encode character u'\u2013'

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • thomas Armstrong

    #1

    'ascii' codec can't encode character u'\u2013'

    Hi

    Using Python 2.3.4 + Feedparser 3.3 (a library to parse XML documents)

    I'm trying to parse a UTF-8 document with special characters like
    acute-accent vowels:
    --------
    <?xml version="1.0" encoding="UTF-8" standalone="yes "?>
    ....
    -------

    But I get this error message:
    -------
    UnicodeEncodeEr ror: 'ascii' codec can't encode character u'\u2013' in
    position 122: ordinal not in range(128)
    -------

    when trying to execute a MySQL query:
    ----
    query = "UPDATE blogs_news SET text = '" + text_extrated + "'WHERE
    id='" + id + "'"
    cursor.execute (query) #<--- error line
    ----

    I tried with:
    -------
    text_extrated = text_extrated.e ncode('iso-8859-1') #<--- error line
    query = "UPDATE blogs_news SET text = '" + text_extrated + "'WHERE
    id='" + id + "'"
    cursor.execute (query)
    -------

    But I get this error:
    ------
    UnicodeEncodeEr ror: 'latin-1' codec can't encode character u'\u2013'
    in position 92: ordinal not in range(256)
    -----

    I also tried with:
    ----
    text_extrated = re.sub(u'\u2013 ', '-' , text_extrated)
    query = "UPDATE blogs_news SET text = '" + text_extrated + "'WHERE
    id='" + id + "'"
    cursor.execute (query)
    -----

    It works, but I don't want to substitute each special character,
    because there are
    always forgotten ones which can crack the program.

    Any suggestion to fix it? Thank you very much.
  • deelan

    #2
    Re: 'ascii' codec can't encode character u'\u2013'

    thomas Armstrong wrote:
    (...)[color=blue]
    > when trying to execute a MySQL query:
    > ----
    > query = "UPDATE blogs_news SET text = '" + text_extrated + "'WHERE
    > id='" + id + "'"
    > cursor.execute (query) #<--- error line
    > ----[/color]

    well, to start it's not the best way to do an update,
    try this instead:

    query = "UPDATE blogs_news SET text = %s WHERE id=%s"
    cursor.execute( query, (text_extrated, id))

    so mysqldb will take care to quote text_extrated automatically. this
    may not not your problem, but it's considered "good style" when dealing
    with dbs.

    apart for this, IIRC feedparser returns text as unicode strings, and
    you correctly tried to encode those as latin-1 str objects before to
    pass it to mysql, but not all glyphs in the orginal utf-8 feed can be
    translated to latin-1. the charecter set of latin-1 is very thin
    compared to the utf-8.

    you have to decide:

    * switch your mysql db to utf-8 and encode stuff before
    insertion to UTF-8

    * lose those characters that cannot be mapped into latin-1,
    using the:

    text_extrated.e ncode('latin-1', errors='replace ')

    so unrecognized chars will be replaced by ?

    also, mysqldb has some support to manage unicode objects directly, but
    things changed a bit during recent releases so i cannot be precise in
    this regard.

    HTH.

    --
    deelan, #1 fan of adriana lima!
    <http://www.deelan.com/>




    Comment

    • thomas Armstrong

      #3
      Re: 'ascii' codec can't encode character u'\u2013'

      Hi.

      Thank you both for your answers.

      Finally I changed my MySQL table to UTF-8 and changed the structure
      of the query (with '%s').

      It works. Thank you very much.

      2005/9/30, deelan <ggg@zzz.it>:[color=blue]
      > thomas Armstrong wrote:
      > (...)[color=green]
      > > when trying to execute a MySQL query:
      > > ----
      > > query = "UPDATE blogs_news SET text = '" + text_extrated + "'WHERE
      > > id='" + id + "'"
      > > cursor.execute (query) #<--- error line
      > > ----[/color]
      >
      > well, to start it's not the best way to do an update,
      > try this instead:
      >
      > query = "UPDATE blogs_news SET text = %s WHERE id=%s"
      > cursor.execute( query, (text_extrated, id))
      >
      > so mysqldb will take care to quote text_extrated automatically. this
      > may not not your problem, but it's considered "good style" when dealing
      > with dbs.
      >
      > apart for this, IIRC feedparser returns text as unicode strings, and
      > you correctly tried to encode those as latin-1 str objects before to
      > pass it to mysql, but not all glyphs in the orginal utf-8 feed can be
      > translated to latin-1. the charecter set of latin-1 is very thin
      > compared to the utf-8.
      >
      > you have to decide:
      >
      > * switch your mysql db to utf-8 and encode stuff before
      > insertion to UTF-8
      >
      > * lose those characters that cannot be mapped into latin-1,
      > using the:
      >
      > text_extrated.e ncode('latin-1', errors='replace ')
      >
      > so unrecognized chars will be replaced by ?
      >
      > also, mysqldb has some support to manage unicode objects directly, but
      > things changed a bit during recent releases so i cannot be precise in
      > this regard.
      >
      > HTH.
      >
      > --
      > deelan, #1 fan of adriana lima!
      > <http://www.deelan.com/>
      >
      >
      >
      >
      > --
      > http://mail.python.org/mailman/listinfo/python-list
      >[/color]

      Comment

      • John J. Lee

        #4
        Re: 'ascii' codec can't encode character u'\u2013'

        deelan <ggg@zzz.it> writes:
        [...][color=blue]
        > query = "UPDATE blogs_news SET text = %s WHERE id=%s"
        > cursor.execute( query, (text_extrated, id))
        >
        > so mysqldb will take care to quote text_extrated automatically. this
        > may not not your problem, but it's considered "good style" when dealing
        > with dbs.[/color]
        [...]

        More than just good style: it prevents SQL injection attacks that
        could otherwise allow people to do bad things to your databases.


        John

        Comment

        Working...