Ascii codec can't encode

L

luca72

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
 
U

Ulrich Eckhardt

luca72 said:
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
 
U

Ulrich Eckhardt

luca72 said:
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
 
L

luca72

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
 
M

Marc 'BlackJack' Rintsch

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
 
L

luca72

the arror are:
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
 
U

Ulrich Eckhardt

luca72 said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top