Comment puis-je diviser une chaîne de caractères Unicode sur les points de code en python? (Par exemple. \ U00B7 ou \ u2022)?

StackOverflow https://stackoverflow.com/questions/8368469

Question

J'ai essayé tout je pouvais penser à ...

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

Rien ne fonctionnait

Le problème est que je veux diviser sur des caractères spéciaux.

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

S'il vous plaît aide.

Merci à l'avance.

Était-ce utile?

La solution

Considérez:

>>> 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']
>>> 

Cela devrait faire la différence entre text.split('\u2022') et text.split(u'\u2022') claire.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top