what is this UnicodeDecodeError:....?

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Tue Oct 10 16:32:28 EDT 2006


In <1160504403.384348.253650 at k70g2000cwa.googlegroups.com>, kath wrote:

> To identify the date field in different files I have created a file
> called _globals where I keep all aliases for DATE in a array called
> 'alias_DATE'.
> 
> Array alias_DATE looks like,
> 
> alias_DATE=['TRADEDATE', 'Accounting Date', 'Date de VL','Datum',
> 'Kurs-datum', 'Date', 'Fecha Datos', 'Calculation Date', 'ClosingDate',
> 'Pricing Date', 'NAV Date', 'NAVDate', 'NAVDATE', 'ValuationDate',
> 'Datestamp', 'Fecha de Valoración', 'Kurs-','datum',
> """Kurs-\ndatum""", "Kurs-\ndatum"]
> 
> Now I want the index of the column where date is there.  I followed the
> with followin code.
> 
> 
>>>> b=xlrd.open_workbook('Santander_051206.xls')
>>>> sh=b.sheet_by_index(0)
>>>> sh.cell_value(rowx=0, colx=11)
> u'Fecha de Valoraci\xf3n'
>>>> val=sh.cell_value(rowx=0, colx=11)
>>>> val
> u'Fecha de Valoraci\xf3n'
>>>> print val
> Fecha de Valoración
>>>> import _globals		# the file where I have stored my 'alias_DATE' array
>>>> _globals.alias_DATE.index(val)
> Traceback (most recent call last):
>   File "<interactive input>", line 1, in ?
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xf3 in position
> 17: ordinal not in range(128)
>>>>
> 
> Though I have matching value in the array, why I am getting this error.

Because you are trying to compare a unicode string `val` with a byte
string in the list. The unicode string will be converted to a byte string
for this comparison with the default encoding: ASCII.  But 'ó' is not
contained in ASCII.

> Can any one please tell me why is this error, and how to get rid of
> this error. Because I have some files which containing some more
> special characters. 

Either use an unicode string in the list search too or explicitly encode
the unicode string `val` with the appropriate encoding before using it to
search the list.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list