Ascii codec can't encode

Discussion in 'Python' started by luca72, Oct 30, 2008.

  1. luca72

    luca72 Guest

    hello i have this problem:
    UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in
    position 8: ordinal not in range(128)
    Generally i solve the problem inserting :
    # -*- coding: ISO-8859-1 -*-
    at the top of the file but now he don't work can you help me

    thanks

    Luca
    luca72, Oct 30, 2008
    #1
    1. Advertising

  2. luca72 wrote:
    > hello i have this problem:
    > UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in
    > position 8: ordinal not in range(128)


    This is the result of transcoding a Unicode string to ASCII, where the
    Unicode string contains a character that is not representable in ASCII.
    This happens to me when I try to print such a character because my console
    is set to ASCII.

    > Generally i solve the problem inserting :
    > # -*- coding: ISO-8859-1 -*-
    > at the top of the file but now he don't work can you help me


    This only tells Python that your sourcecode uses Latin 1 as encoding. It
    doesn't affect other files or any input or output streams.

    What code exactly triggers the error?

    Uli

    --
    Sator Laser GmbH
    Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
    Ulrich Eckhardt, Oct 30, 2008
    #2
    1. Advertising

  3. luca72

    luca72 Guest

    Hy the code is this:

    Pok\xe9mon

    Luca
    luca72, Oct 30, 2008
    #3
  4. luca72

    luca72 Guest

    On 30 Ott, 10:27, luca72 <> wrote:
    > Hy the code is this:
    >
    > Pok\xe9mon
    >
    > Luca


    Sorry is the é
    luca72, Oct 30, 2008
    #4
  5. luca72 wrote:
    > Hy the code is this:
    >
    > Pok\xe9mon


    That's not what I meant, I meant a piece of Python source code. This piece
    has to be large enough to demonstrate the problem but with everything else
    removed. The point is that guessing what is wrong in your program is just
    futile; In order to help, people need information.

    Uli

    --
    Sator Laser GmbH
    Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
    Ulrich Eckhardt, Oct 30, 2008
    #5
  6. luca72

    luca72 Guest

    Hello Again

    the code is this
    # -*- coding: ISO-8859-1 -*-
    from BeautifulSoup import BeautifulSoup
    import urllib
    sito = urllib.urlopen('http://text.net/')
    esamino = BeautifulSoup(sito)
    luca = esamino.findAll('tr', align='center')
    lunghezza = len(luca)
    messaggio_per_scar = open('me', 'wb')
    file_ricerca = open('ri', 'wb')
    for dati in range(lunghezza):
    gino = luca[dati]
    test = gino.findAll(text=True)
    print test
    valori_a = gino.find("a")["onclick"].split("'")
    print valori_a
    nome_boot = valori_a[1]
    print nome_boot
    frase_scar = '/msg '+nome_boot+' xdcc send '+test[0]+'\n\r'
    messaggio_per_scar.write(frase_scar)
    nome = test[2]
    if nome ==' ':
    nome = test[4]
    frase_ric = nome+' '+test[1]+' '+test[0]+'\n\r'
    file_ricerca.write(frase_ric) #here i have the error
    messaggio_per_scar.close()
    file_ricerca.close()

    Regards

    Luca
    luca72, Oct 30, 2008
    #6
  7. On Thu, 30 Oct 2008 03:01:07 -0700, luca72 wrote:

    > the code is this
    > # -*- coding: ISO-8859-1 -*-
    > from BeautifulSoup import BeautifulSoup import urllib
    > sito = urllib.urlopen('http://text.net/') esamino = BeautifulSoup(sito)
    > luca = esamino.findAll('tr', align='center') lunghezza = len(luca)
    > messaggio_per_scar = open('me', 'wb') file_ricerca = open('ri', 'wb')
    > for dati in range(lunghezza):
    > gino = luca[dati]
    > test = gino.findAll(text=True)
    > print test
    > valori_a = gino.find("a")["onclick"].split("'") print valori_a
    > nome_boot = valori_a[1]
    > print nome_boot
    > frase_scar = '/msg '+nome_boot+' xdcc send '+test[0]+'\n\r'
    > messaggio_per_scar.write(frase_scar)
    > nome = test[2]
    > if nome ==' ':
    > nome = test[4]
    > frase_ric = nome+' '+test[1]+' '+test[0]+'\n\r'
    > file_ricerca.write(frase_ric) #here i have the error
    > messaggio_per_scar.close()
    > file_ricerca.close()


    And which of those line raised the exception? Full traceback please.

    I guess it is one of the ``print``\s because `BeatifulSoup` returns
    `unicode` objects which may lead to that exception when printed and the
    output encoding can not be determined.

    Ciao,
    Marc 'BlackJack' Rintsch
    Marc 'BlackJack' Rintsch, Oct 30, 2008
    #7
  8. luca72

    luca72 Guest

    the arror are:

    >>> Error in sys.excepthook:

    Traceback (most recent call last):
    File "/usr/lib/python2.5/site-packages/eric4/DebugClients/Python/
    DebugClientBase.py", line 1006, in __unhandled_exception
    self.mainThread.user_exception(None, (exctype,excval,exctb), 1)
    File "/usr/lib/python2.5/site-packages/eric4/DebugClients/Python/
    DebugBase.py", line 538, in user_exception
    self._dbgClient.eventLoop()
    File "/usr/lib/python2.5/site-packages/eric4/DebugClients/Python/
    DebugClientBase.py", line 921, in eventLoop
    self.readReady(self.readstream.fileno())
    File "/usr/lib/python2.5/site-packages/eric4/DebugClients/Python/
    AsyncIO.py", line 67, in readReady
    self.handleLine(s)
    File "/usr/lib/python2.5/site-packages/eric4/DebugClients/Python/
    DebugClientBase.py", line 311, in handleLine
    self.__dumpVariables(int(frmnr), int(scope), filter)
    File "/usr/lib/python2.5/site-packages/eric4/DebugClients/Python/
    DebugClientBase.py", line 1128, in __dumpVariables
    vlist = self.__formatVariablesList(keylist, dict, scope, filter)
    File "/usr/lib/python2.5/site-packages/eric4/DebugClients/Python/
    DebugClientBase.py", line 1594, in __formatVariablesList
    valtypestr = unicode(type(value))[1:-1]
    TypeError: unbound method __unicode__() must be called with
    NavigableString instance as first argument (got nothing instead)

    Original exception was:
    Traceback (most recent call last):
    File "/usr/lib/python2.5/site-packages/eric4/DebugClients/Python/
    DebugClient.py", line 38, in <module>
    debugClient.main()
    File "/usr/lib/python2.5/site-packages/eric4/DebugClients/Python/
    DebugClientBase.py", line 1900, in main
    self.__interact()
    File "/usr/lib/python2.5/site-packages/eric4/DebugClients/Python/
    DebugClientBase.py", line 897, in __interact
    self.eventLoop()
    File "/usr/lib/python2.5/site-packages/eric4/DebugClients/Python/
    DebugClientBase.py", line 921, in eventLoop
    self.readReady(self.readstream.fileno())
    File "/usr/lib/python2.5/site-packages/eric4/DebugClients/Python/
    AsyncIO.py", line 67, in readReady
    self.handleLine(s)
    File "/usr/lib/python2.5/site-packages/eric4/DebugClients/Python/
    DebugClientBase.py", line 445, in handleLine
    execfile(sys.argv[0], self.debugMod.__dict__)
    File "/home/luca11/Desktop/prova.py", line 36, in <module>
    file_ricerca.write(frase_ric)
    UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in
    position 8: ordinal not in range(128)

    I have solve in this way:

    file_ricerca = codecs.open('ri', 'wb', 'ISO-8859-15', 'repalce')

    That is not exact what i want because instead of the 'é' i get '?' but
    the loop contine.
    Thanks

    Luca
    luca72, Oct 30, 2008
    #8
  9. luca72 wrote:
    > UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in
    > position 8: ordinal not in range(128)
    >
    > I have solve in this way:
    >
    > file_ricerca = codecs.open('ri', 'wb', 'ISO-8859-15', 'repalce')


    That should be 'replace' instead of 'repalce', I assume you just mistyped it
    here. In any case, you could use 'UTF-8', which is a good default encoding
    and which further allows the full range of Unicode characters.

    > That is not exact what i want because instead of the 'é' i get '?' but
    > the loop contine.


    Hmmm, weird, I'd say that ISO-8859-15 should support the accented 'é'.
    However, this could also be caused by how you look at the file. If the
    editor you use to open it uses a different encoding, it will display
    garbage.

    Uli

    --
    Sator Laser GmbH
    Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
    Ulrich Eckhardt, Oct 30, 2008
    #9
    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. oziko
    Replies:
    1
    Views:
    509
    Leif K-Brooks
    Aug 17, 2004
  2. Martin Slouf
    Replies:
    6
    Views:
    912
    Martin Slouf
    Aug 18, 2004
  3. Ben Last
    Replies:
    0
    Views:
    413
    Ben Last
    Aug 17, 2004
  4. oziko
    Replies:
    2
    Views:
    11,387
    Diez B. Roggisch
    Aug 17, 2004
  5. thomas Armstrong

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

    thomas Armstrong, Sep 30, 2005, in forum: Python
    Replies:
    3
    Views:
    4,449
    John J. Lee
    Sep 30, 2005
Loading...

Share This Page