How to Split Chinese Character with backslash representation?

Wijaya Edward ewijaya at i2r.a-star.edu.sg
Fri Oct 27 01:09:36 EDT 2006


Thanks but my intention is to strictly use regex.
Since there are separator I need to include as delimiter
Especially for the case like this:


>>> str = '\xc5\xeb\xc7\xd5\xbc--FOO--BAR'
>>> field = list(str)
>>> print field
['\xc5', '\xeb', '\xc7', '\xd5', '\xbc', '-', '-', 'F', 'O', 'O', '-', '-', 'B', 'A', 'R']


What we want as the output is this instead:
['\xc5', '\xeb', '\xc7', '\xd5', '\xbc','FOO','BAR]


What's the best way to do it?


-- Edward WIJAYA
SINGAPORE

________________________________

From: python-list-bounces+ewijaya=i2r.a-star.edu.sg at python.org on behalf of Cameron Walsh
Sent: Fri 10/27/2006 12:03 PM
To: python-list at python.org
Subject: Re: How to Split Chinese Character with backslash representation?



Wijaya Edward wrote:
> Hi all,
>
> I was trying to split a string that
> represent chinese characters below:
>

>

>>>> str = '\xc5\xeb\xc7\xd5\xbc'
>>>> print str2,
> ???
>>>> fields2 = split(r'\\',str)
>>>> print fields2,
> ['\xc5\xeb\xc7\xd5\xbc']
>
> But why the split function here doesn't seem
> to do the job for obtaining the desired result:
>

> ['\xc5','\xeb','\xc7','\xd5','\xbc']
>


Depends on what you want to do with them:

 >>> string = '\xc5\xeb\xc7\xd5\xbc'
 >>> for char in string:
         print char


Å
ë
Ç
Õ
¼
 >>> list_of_characters = list(string)
 >>> list_of_characters
['\xc5', '\xeb', '\xc7', '\xd5', '\xbc']
 >>> for char in string:
         char


'\xc5'
'\xeb'
'\xc7'
'\xd5'
'\xbc'
 >>> for char in list_of_characters:
         print char


Å
ë
Ç
Õ
¼
 >>> string[3]
'\xd5'
 >>> string[1:3]
'\xeb\xc7'

Basically, you characters are already separated into a list of
characters, that's effectively what a string is (but with a few more
methods applicable only to lists of characters, not to other lists).
--
http://mail.python.org/mailman/listinfo/python-list



------------ Institute For Infocomm Research - Disclaimer -------------
This email is confidential and may be privileged.  If you are not the intended recipient, please delete it and notify us immediately. Please do not copy or use it for any purpose, or disclose its contents to any other person. Thank you.
--------------------------------------------------------



More information about the Python-list mailing list