re.finditer() skips unicode into selection

darpan6aya akshay.ksth at gmail.com
Wed Jun 26 23:26:40 EDT 2013


Thanks MRAB, your suggestion worked. But then it brought an error 

    'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)

I corrected this by encoding it to 'utf-8'. The code looks like this now. 

        pattern = ur'(?u)\w+' 

        def __init__(self, *args):
            QSyntaxHighlighter.__init__(self, *args)
            self.dict = None
            
        def setDict(self, dict):
            self.dict = dict
            
        def highlightBlock(self, text):
            if not self.dict:
                return
            text = unicode(text)
            format = QTextCharFormat()
            format.setUnderlineColor(Qt.red)
            format.setUnderlineStyle(QTextCharFormat.SpellCheckUnderline)
            
            unicode_pattern=re.compile(self.pattern,re.UNICODE)
                    
            for word_object in unicode_pattern.finditer(text):
                if not self.dict.spell(word_object.group().encode('utf-8')):
                    print word_object.group().encode('utf-8')
                    self.setFormat(word_object.start(), word_object.end() - word_object.start(), format)

The problem now is that all the vowels are separated from the root word, such that if you type मेरो, the म and े are printed separately. (the े appears as a box instead). What am I doing wrong?

Like this.

मेरो नाम रुपा हो।



More information about the Python-list mailing list