By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,587 Members | 1,677 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,587 IT Pros & Developers. It's quick & easy.

Ascii codec can't encode

P: n/a
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
Oct 30 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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

Oct 30 '08 #2

P: n/a
Hy the code is this:

Pok\xe9mon

Luca
Oct 30 '08 #3

P: n/a
On 30 Ott, 10:27, luca72 <lucabe...@libero.itwrote:
Hy the code is this:

Pok\xe9mon

Luca
Sorry is the
Oct 30 '08 #4

P: n/a
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
Oct 30 '08 #5

P: n/a
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
Oct 30 '08 #6

P: n/a
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
Oct 30 '08 #7

P: n/a
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

Oct 30 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.