How can I properly parse a string into grapheme clusters using python 2.7?
<p>I'm trying to find a way using python 2.7.1 to parse a string into grapheme clusters.  For example, the string:</p>

<pre><code>details = u"Hello í ¼í·¦í ¼í·¹í ¼í·»í ¼í·ª"
</code></pre>

<p>I believe should be parsed as:</p>

<pre><code>[u"H", u"e", u"l", u"l", u"o", u"\U0001f1e6\U0001f1f9", u"\U0001f1fb\U0001f1ea"]
</code></pre>

<p>I was using <code>grapheme_clusters</code> from the <a href="https://bitbucket.org/emptypage/uniseg-python" rel="nofollow">uniseg</a> library, but this produces:</p>

<pre><code>[u"H", u"e", u"l", u"l", u"o", u"\U0001f1e6\U0001f1f9\U0001f1fb\U0001f1ea"]
</code></pre>

<p>I have a hard requirement on using 2.7.1.  I know unicode support is better in python 3.X.</p>

<p>1) Is my interpretation of the way this string should be parsed correct?<br>
2) Is there a way to do this with python 2.7?<br>
3) Is this actually any easier in python 3.X?</p>

<p>Thanks for any help.</p>

<p>This behavior used to be correct, but the rules changed.</p>

<p>As of uniseg version 0.7.1 (current as of this post), the uniseg documentation refers to an outdated version of the Unicode grapheme cluster boundary rules, given in <a href="http://www.unicode.org/reports/tr29/tr29-21.html" rel="nofollow">Unicode Standard Annex #29 Version 21</a>. This version includes the rule</p>

<blockquote>
  <p>Do not break between regional indicator symbols.</p>
</blockquote>

<p>where the most recent version of the Unicode grapheme cluster boundary rules, given in <a href="http://www.unicode.org/reports/tr29/tr29-29.html" rel="nofollow">Unicode Standard Annex #29 Version 29</a> says</p>

<blockquote>
  <p>Do not break within emoji flag sequences. That is, do not break between regional indicator (RI) symbols if there is an odd number of RI characters before the break point.</p>
</blockquote>

<p>You could file a bug report with uniseg, or perhaps a different library would have a more up-to-date implementation. The uniseg bitbucket page links to a few alternatives, such as <a href="https://pypi.python.org/pypi/PyICU" rel="nofollow">PyICU</a>.</p>

python python-2.7 python-3.x unicode