Ascii codec can't encode

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

    Ascii codec can't encode

    hello i have this problem:
    UnicodeEncodeEr ror: '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
  • Ulrich Eckhardt

    #2
    Re: Ascii codec can't encode

    luca72 wrote:
    hello i have this problem:
    UnicodeEncodeEr ror: '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ühr er: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

    Comment

    • luca72

      #3
      Re: Ascii codec can't encode

      Hy the code is this:

      Pok\xe9mon

      Luca

      Comment

      • luca72

        #4
        Re: Ascii codec can't encode

        On 30 Ott, 10:27, luca72 <lucabe...@libe ro.itwrote:
        Hy the code is this:
        >
        Pok\xe9mon
        >
        Luca
        Sorry is the é

        Comment

        • luca72

          #5
          Re: Ascii codec can't encode

          Hello Again

          the code is this
          # -*- coding: ISO-8859-1 -*-
          from BeautifulSoup import BeautifulSoup
          import urllib
          sito = urllib.urlopen( 'http://text.net/')
          esamino = BeautifulSoup(s ito)
          luca = esamino.findAll ('tr', align='center')
          lunghezza = len(luca)
          messaggio_per_s car = open('me', 'wb')
          file_ricerca = open('ri', 'wb')
          for dati in range(lunghezza ):
          gino = luca[dati]
          test = gino.findAll(te xt=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_s car.write(frase _scar)
          nome = test[2]
          if nome ==' ':
          nome = test[4]
          frase_ric = nome+' '+test[1]+' '+test[0]+'\n\r'
          file_ricerca.wr ite(frase_ric) #here i have the error
          messaggio_per_s car.close()
          file_ricerca.cl ose()

          Regards

          Luca

          Comment

          • Marc 'BlackJack' Rintsch

            #6
            Re: Ascii codec can't encode

            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(s ito)
            luca = esamino.findAll ('tr', align='center') lunghezza = len(luca)
            messaggio_per_s car = open('me', 'wb') file_ricerca = open('ri', 'wb')
            for dati in range(lunghezza ):
            gino = luca[dati]
            test = gino.findAll(te xt=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_s car.write(frase _scar)
            nome = test[2]
            if nome ==' ':
            nome = test[4]
            frase_ric = nome+' '+test[1]+' '+test[0]+'\n\r'
            file_ricerca.wr ite(frase_ric) #here i have the error
            messaggio_per_s car.close()
            file_ricerca.cl ose()
            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

            Comment

            • luca72

              #7
              Re: Ascii codec can't encode

              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_exc eption
              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.__dumpVari ables(int(frmnr ), int(scope), filter)
              File "/usr/lib/python2.5/site-packages/eric4/DebugClients/Python/
              DebugClientBase .py", line 1128, in __dumpVariables
              vlist = self.__formatVa riablesList(key list, dict, scope, filter)
              File "/usr/lib/python2.5/site-packages/eric4/DebugClients/Python/
              DebugClientBase .py", line 1594, in __formatVariabl esList
              valtypestr = unicode(type(va lue))[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.mai n()
              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.ar gv[0], self.debugMod._ _dict__)
              File "/home/luca11/Desktop/prova.py", line 36, in <module>
              file_ricerca.wr ite(frase_ric)
              UnicodeEncodeEr ror: '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


              Comment

              • Ulrich Eckhardt

                #8
                Re: Ascii codec can't encode

                luca72 wrote:
                UnicodeEncodeEr ror: '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ühr er: Thorsten Föcking, Amtsgericht Hamburg HR B62 932

                Comment

                Working...