Created
June 2, 2013 17:46
-
-
Save dpk/5694265 to your computer and use it in GitHub Desktop.
Python: iterate over the graphemes in a string.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unicodedata as u | |
def itergraphemes(str): | |
def modifierp(char): return u.category(char)[0] == 'M' | |
start = 0 | |
for end, char in enumerate(str): | |
if not modifierp(char) and not start == end: | |
yield str[start:end] | |
start = end | |
yield str[start:] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://grapheme.readthedocs.io/en/latest/grapheme.html