Issue
I created tree in treelib and called
tree.show()
Ufortunately instead of tree it prints something like
b'Hips\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 LeftUpLeg\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 LeftLeg\n\xe2\x94\x82
I saw such b-prefixed strings many time but don't know what do they mean. Something like incompatibility between Python 2 and 3?
How to fix?
Solution
It is a bug, partly addressed.
See this issue raised on github.
In your Python installation, go to Lib\site-packages\treelib
and look at tree.py
. Since 1.7.0, in def show
, the last lines are:
if stdout:
print(self._reader.encode("utf-8"))
else:
return self._reader
The problem is in this line, which doesn't respect the console's default encoding:
print(self._reader.encode('utf-8'))
Instead you want to print the reader itself, which you can get by calling tree.show(stdout=False)
from your application (no need to edit the library source):
. . .
print(tree.show(stdout=False))
Printing Unicode strings directly should encode them in the console's default encoding, so as long as your console supports the line drawing characters, it will print fine.
If you have an earlier version of treelib, you can edit the library tree.py
file and change the last line to be:
print(self.reader)
This works like:
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from treelib import Tree
>>> tree=Tree()
>>> tree.create_node('A','a')
Node(tag='A', identifier='a', data=None)
>>> tree.create_node('B','b',parent='a')
Node(tag='B', identifier='b', data=None)
>>> tree.show()
A
└── B
It's probably not the only bug. The author isn't following the rule of encoding/decoding at the I/O boundaries of a program and work with Unicode internally.
Answered By - Mark Tolonen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.