Why does the "".join(r) do this?

moma moma at example.net
Thu May 20 12:24:15 EDT 2004


Jim Hefferon wrote:
> Hello,
> 
> I'm getting an error join-ing strings and wonder if someone can
> explain why the function is behaving this way?  If I .join in a string
> that contains a high character then I get an ascii codec decoding
> error.  (The code below illustrates.)  Why doesn't it just
> concatenate?
> 
> I'm building up a web page by stuffing an array and then doing
> "".join(r) at
> the end.  I intend to later encode it as 'latin1', so I'd like it to
> just concatenate.  While I can work around this error, the reason for
> it escapes me.
> 
> Thanks,
> Jim
> 
> ================= program: try.py
> #!/usr/bin/python2.3 -u
> t="abc"+chr(174)+"def"
> print(u"next: %s :there" % (t.decode('latin1'),))
> print t
> r=["x",'y',u'z']
> r.append(t)
> k="".join(r)
> print k
> 
> ================== command line (on my screen between the first abc
> and def is
>   a circle-R, while between the second two is a black oval with a
> white
>   question mark, in case anyone cares):
> jim at joshua:~$ ./try.py
> next: abc®def :there
> abc�def
> Traceback (most recent call last):
>   File "./try.py", line 7, in ?
>     k="".join(r)
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xae in position
> 3: ordinal not in range(128)

What about unichr()  ?


#!/usr/bin/python2.3 -u
t="abc"+unichr(174)+"def"
print t
print(u"next: %s :there" % (t),)
print t
r=["x",'y',u'z']
r.append(t)
# k=u"".join(r)
k="".join(r)
print k


// moma
    http://www.futuredesktop.org



More information about the Python-list mailing list