简体   繁体   中英

How do I split a unicode string on code points in python? (eg. \u00B7 or \u2022)?

I tried everything I could think of...

1. unicode_obj.split('\u2022')
2. re.split(r'\u2022', unicode_object)
3. re.split(r'(?iu)\u2022', unicode_object)

Nothing worked

The problem is that I want to split on special characters.

example string : u'<special char like middot:\u00b7 or bullet:\u2022> sdfhsdf <repeat special char> sdfjhdgndujhfsgkljng <repeat special char> ... etc'

Please help.

Thanks in advance.

Consider:

>>> print '\u2022'
\u2022
>>> print len('\u2022')
6
>>> import unicodedata
>>> map(unicodedata.name, '\u2022'.decode('ascii'))
['REVERSE SOLIDUS', 'LATIN SMALL LETTER U', 'DIGIT TWO', 'DIGIT ZERO', 'DIGIT TWO', 'DIGIT TWO']
>>> 

vs:

>>> print u'\u2022'
•
>>> print len(u'\u2022')
1
>>> map(unicodedata.name, u'\u2022')
['BULLET']
>>> 

This should make the difference between text.split('\•') and text.split(u'\•') clear.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM