473,396 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 2636
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Edward K. Ream | last post by:
Am I reading pep 277 correctly? On Windows NT/XP, should filenames always be converted to Unicode using the mbcs encoding? For example, myFile = unicode(__file__, "mbcs", "strict") This...
19
by: Gerson Kurz | last post by:
AAAAAAAARG I hate the way python handles unicode. Here is a nice problem for y'all to enjoy: say you have a variable thats unicode directory = u"c:\temp" Its unicode not because you want it...
4
by: David Siroky | last post by:
Hi! I need to enlighten myself in Python unicode speed and implementation. My platform is AMD Athlon@1300 (x86-32), Debian, Python 2.4. First a simple example (and time results): x =...
4
by: Petr Jakes | last post by:
Hi, I am using Python 2.4.3 on Fedora Core4 and "Eric3" Python IDE .. Below mentioned code works fine in the Eric3 environment. While trying to start it from the command line, it returns: ...
13
by: gabor | last post by:
hi, from the documentation (http://docs.python.org/lib/os-file-dir.html) for os.listdir: "On Windows NT/2k/XP and Unix, if path is a Unicode object, the result will be a list of Unicode...
24
by: Donn Ingle | last post by:
Hello, I hope someone can illuminate this situation for me. Here's the nutshell: 1. On start I call locale.setlocale(locale.LC_ALL,''), the getlocale. 2. If this returns "C" or anything...
2
by: nihilium | last post by:
A document contains text and notes below it. Both sections contains unicode. I want to add a disclaimer above the notelist. import re f = open('C:/example.txt', 'r') source = f.read() patt =...
1
by: Peter Robinson | last post by:
Dear list I am at my wits end on what seemed a very simple task: I have some greek text, nicely encoded in utf8, going in and out of a xml database, being passed over and beautifully displayed...
14
by: Russell E. Owen | last post by:
I have code like this: except Exception, e: self.setState(self.Failed, str(e)) which fails if the exception contains a unicode argument. I did, of course, try unicode(e) but that fails. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.