473,573 Members | 2,814 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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"Hell o:afddfdsfa")

y = u'\n'.join(x)
print("Unicode Call worked!")
except Exception, err:
print("Exceptio n 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 2644
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"Hell o: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
6477
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 seems to work, and I'm wondering whether there are any other details to consider. My experiments with Idle for Python 2.2 indicate that os.path.join...
19
11866
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 to, but because its for example read from _winreg which returns unicode. You do an os.listdir(directory). Note that all filenames returned are now...
4
1943
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 = "a"*50000000 real 0m0.195s
4
3312
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: Traceback (most recent call last): File "pokus_1.py", line 5, in ? print str(a) UnicodeEncodeError: 'ascii' codec can't encode character u'\xc1' in
13
2951
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 objects." i'm on Unix. (linux, ubuntu edgy)
24
3355
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 without 'utf8' in it, then things start to go downhill: 2a. The app assumes unicode objects internally. i.e. Whenever there is
2
1474
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 = re.compile(r'(.*)+\s?(?<!):\n]*', re.DOTALL) # finding the main text text= patt.search(source) # returns text containing unicode patt =...
1
1451
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 on the web. For example: the most common greek word of all 'kai' (or καιif your mailer can see utf8) So all I want to do is: step through...
14
1485
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 following works, but seems rather messy: except Exception, e:
0
7781
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
7699
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7788
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6421
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5594
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5292
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2216
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1304
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1041
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.