2
votes

How can I concatenate a string and a Unicode and get a string as a result? I tried the following. But this is obviously not correct.

>>> s = 'jklö'
>>> u = u'äpoi'
>>> print s+u

Traceback (most recent call last): File "", line 1, in UnicodeDecodeError: 'ascii' codec can't decode byte 0x94 in position 3: ordinal not in range(128)

>>> unicode(s, 'latin-1')+u
u'jkl\x94\xe4poi'
>>> (unicode(s, 'latin1')+u).encode('latin1')
'jkl\x94\xe4poi'
>>> print (unicode(s, 'latin1')+u).encode('latin1')
jklöõpoi
3
Why is it obviously not correct? Python tried to decode your bytestring using the default (ASCII), which fails. Then you decode s explicitly (using unicode()), so it can append the results. That's the correct way to concatenate unicode data; by using only unicode objects. - Martijn Pieters
convert string to unicode first? - Jimmy Kane
@MartijnPieters: it is not correct because the ä in the final output became a õ - domruf

3 Answers

2
votes

You are trying to decode s from latin-1 but your terminal is not using latin-1 encoding; check sys.stdin.encoding to find out what it is.

For example, I have cp850 by default:

>>> import sys
>>> sys.stdin.encoding
'cp850'
>>> s = 'jklö'
>>> u = u'äpoi'
>>> s
'jkl\x94'
>>> u
u'\xe4poi'
>>> print unicode(s, 'cp850') + u
jklöäpoi
>>> print (unicode(s, 'cp850') + u).encode('cp850')
jklöäpoi
1
votes

Please read this and keep this under your pillow. This is extreme valuable documentation

http://farmdev.com/talks/unicode/

http://docs.python.org/2/howto/unicode.html

0
votes

This seems to work:

>>> l = s + u.encode('cp1252')
>>> print l
jklöäpoi
>>> type(l)
<type 'str'>