468,284 Members | 1,612 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,284 developers. It's quick & easy.

Unicode lists and join (python 2.2.3)

Hello,
I have a list of strings, some of the strings might be unicode. I am
trying to a .join operation on the list and the .join raises a unicode
exception. I am looking for ways to get around this.
I would like to get a unicode string out of the list with all string
elements seperated by '\n'

#!/usr/bin/env python
import sys
import string

try:
x = [u"\xeeabc2:xyz", u"abc3:123"]
u = "\xe7abc"
x.append("%s:%s" % ("xfasfs", u))
x.append(u"Hello:afddfdsfa")

y = u'\n'.join(x)
print("Unicode Call worked!")
except Exception, err:
print("Exception raised %s" % err)

on a related note
Why does this work with no exceptions

x=[]
u = "\xe7abc"
x.append("%s:%s" % ("xfasfs", u))

and this doesnt
x=[]
u = "\xe7abc"
x.append("%s:%s" % (u"xfasfs", u))
Thanks,
Nitin.
Jun 27 '08 #1
1 2459
x = [u"\xeeabc2:xyz", u"abc3:123"]
u = "\xe7abc"
u is not a Unicode string.
x.append("%s:%s" % ("xfasfs", u))
so what you append is not a Unicode string, either.
x.append(u"Hello:afddfdsfa")

y = u'\n'.join(x)
As a consequence, .join tries to convert the byte string to
a Unicode string, and fails, because it contains non-ASCII
bytes.
Why does this work with no exceptions

x=[]
u = "\xe7abc"
x.append("%s:%s" % ("xfasfs", u))
% here is applied to a byte string, with all arguments also byte
strings. The result is a byte string.
>
and this doesnt
x=[]
u = "\xe7abc"
x.append("%s:%s" % (u"xfasfs", u))
% is applied to a byte string, with one argument being a Unicode
string. The result is a Unicode string, where all byte strings
get converted to Unicode. Converting u fails, as it has non-ASCII
bytes in it.

Regards,
Martin
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Edward K. Ream | last post: by
19 posts views Thread by Gerson Kurz | last post: by
4 posts views Thread by David Siroky | last post: by
4 posts views Thread by Petr Jakes | last post: by
2 posts views Thread by nihilium | last post: by
1 post views Thread by Peter Robinson | last post: by
14 posts views Thread by Russell E. Owen | last post: by
reply views Thread by NPC403 | last post: by
2 posts views Thread by MrBee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.