0

I am very tired about all this unicode problems. I need to build graph with object nodes. And I need do show this graph. I have problems with visualization unicode strings. So... To make object as graph node, I need to override equal and hash methods

class VkUser:
    def __init__(self,uid=None,f_name = None,l_name = None,json=None):
        if(json==None):
            self.uid=uid
            self.l_name=l_name
            self.f_name=f_name
        else:
            self.uid = json['uid']
            self.f_name = json['first_name']
            self.l_name = json['last_name']
    def __eq__(self, other):
        if isinstance(other, VkUser):
            return (self.uid == other.uid)
        return NotImplemented
    def __ne__(self, other):
        result = self.__eq__(other)
        if result is NotImplemented:
            return result
        return not result
    def __hash__(self):
        return hash(self.uid)

But, if I want to not Pointers as nodes of the graph, i need to override str

def __str__(self):
        return '%s %s'%(self.f_name,self.l_name)

It works fine, while I have just english letters.

But I have Json input with unicode russian leters, and I need to show them on graph. Somethisg like

Me = VkUser(111,u'\u0410\u043b\u0435\u043a\u0441\u0430\u043d\u0434\u0440',u'\u0410\u043b\u0435\u043a\u0441\u0430\u043d\u0434\u0440')

And now I have got en error

   label=str(label) # this will cause "1" and 1 to be labeled the same
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-8: ordinal not in range(128)

I tried different variants in str

def str(self): return u'%s %s'%(self.f_name,self.l_name) Same error

def __str__(self):
        res = u'%s %s'%(self.f_name,self.l_name)
        return res.encode('utf-8')
...
ValueError: matplotlib display text must have all code points < 128 or use Unicode strings

Help me please, I am rely tired from this.

4

1 に答える 1

0
l = u'\u0410\u043b\u0435\u043a\u0441\u0430\u043d\u0434\u0440'
plot(range(5), label=l)
legend()

2.7 とほぼ最新のマスター (1.4.x) で期待どおりに動作します (これらの文字のフォントがないため、ボックスが表示されます)。

于 2013-09-04T20:01:50.543 に答える