Novice: replacing strings with unicode variables in a list

aine_canby at yahoo.com aine_canby at yahoo.com
Wed Dec 6 05:29:27 EST 2006


aine_ca... at yahoo.com skrev:

> Fredrik Lundh skrev:
>
> > Diez B. Roggisch wrote:
> >
> > > Please provide the full script, and the desired input - then we might be
> > > able to help you.
> >
> > the full *traceback* would pretty useful, too:
> >
> > http://effbot.org/pyfaq/tutor-i-need-help-im-getting-an-error-in-my-program-what-should-i-do.htm
> >
> > my guess is that the OP left out the print statement that causing the error, and the
> > traceback lines that pointed to that print statement:
> >
> > > more test.py
> > uni = u"p\x95l"
> > print uni
> >
> > > python test.py
> > Traceback (most recent call last):
> >   File "test.py", line 2, in <module>
> >     print uni
> >   File "C:\python25\lib\encodings\cp850.py", line 12, in encode
> >     return codecs.charmap_encode(input,errors,encoding_map)
> > UnicodeEncodeError: 'charmap' codec can't encode character u'\x95' in position 1
> > : character maps to <undefined>
> >
> > </F>
>
> Thanks for the replies. OK heres the full code I'm using now -
>
> compare = u"Äis"
> print compare
>
> str = raw_input("Enter music:")
> words = str.split()
>
> uniList=[]
> for word in words:
> 	uni=unicode(word,'latin-1')
> 	uniList.append(uni)
>
> print uniList[0]
>
> if(compare!=uniList[0]):
> 	print "Not the same: " + compare + " " + uniList[0]
>
> This gives the following error -
>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "test.py", line 14, in <module>
>     print uniList[0]
>   File "C:\Python25\lib\encodings\cp850.py", line 12, in encode
>     return codecs.charmap_encode(input,errors,encoding_map)
> UnicodeEncodeError: 'charmap' codec can't encode character u'\x8e' in
> position 0
> : character maps to <undefined>
>
> How come I can print compare but not uniList[0]? What encoding is being
> used to store compare? How can I print out the raw bytes stored in
> compare and uniList[0]?
>
> Thanks again for your help,
>
> Aine.

Cool. It works for me now.

import sys

compare = u"Äis"
print compare

str = raw_input("Enter music:")
words = str.split()

uniList=[]
for word in words:
	uni=unicode(word,sys.stdin.encoding)
	uniList.append(uni)

print uniList[0]

if(compare!=uniList[0]):
	print "Not the same: " + compare + " " + uniList[0]




More information about the Python-list mailing list