perl - Stringifying an Inline::Python::Object-encoded Unicode string -
inline::python::object overloads '""' (stringify) this:
sub __inline_str__ { ($self) = @_; return inline::python::py_has_attr($self, '__str__') ? $self->__str__() : $self; } the __str__() method attempts conversion ascii, meaning if inline::python::object object represents python unicode string, outcome is:
exceptions.unicodeencodeerror: 'ascii' codec can't encode character u'\xe7' in position 6: ordinal not in range(128) @ line 1252
one workaround seems working, replacing $self->__str__() $self->encode('utf8'). don't modifying module this, , subclassing seems considerable challenge. moreover, i'm not 100% sure why fix works, worrying.
i'm quite sure i'm not first person ever needs use python unicode string in perl. how supposed done?
one workaround seems working, replacing $self->str() $self->encode('utf8').
this correct way handle this. code encode utf characters this:
>>> u'\ufdef'.__str__() traceback (most recent call last): file "<stdin>", line 1, in <module> unicodeencodeerror: 'ascii' codec can't encode character u'\ufdef' in position 0: ordinal not in range(128) >>> u'\ufdef'.encode('utf-8') '\xef\xb7\xaf' you want use utf-8 decoder in perl correctly display value.
Comments
Post a Comment