How to Split Chinese Character with backslash representation?

Cameron Walsh cameron.walsh at gmail.com
Fri Oct 27 06:03:31 CEST 2006


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).



More information about the Python-list mailing list