treelib icon indicating copy to clipboard operation
treelib copied to clipboard

tree.show prints binary literal to stdout

Open aholten opened this issue 1 year ago • 4 comments

Issue #82 seems to have been reintroduced at some point after release 1.3.2:

from treelib import Tree
tree = Tree()
tree.create_node("Harry", "harry")  # root node
tree.create_node("Jane", "jane", parent="harry")
tree.create_node("Bill", "bill", parent="harry")
tree.create_node("Diane", "diane", parent="jane")
tree.create_node("Mary", "mary", parent="diane")
tree.create_node("Mark", "mark", parent="jane")
tree.show()

results in: b'Harry\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Bill\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 Jane\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 Diane\n \xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 Mary\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 Mark\n'

aholten avatar Nov 28 '23 19:11 aholten

Also encountered this issue.

As a temporary workaround I changed this line:

            print(self._reader.encode("utf-8"))

To:

            print(self._reader)

In virtual environment it was in .venv/lib/python3.11/site-packages/treelib/tree.py on line 932.

Lab-Brat avatar Dec 02 '23 10:12 Lab-Brat

I'm also affected by this bug. Thanks @lab-Brat for the workaround.

sylock avatar Jan 08 '24 15:01 sylock

There's no need to need to patch the module as a workaround. tree.show() does return the output as a string when told not to write to stdout. So just do that as an argument to print: print(tree.show(stdout=False))

Keep pythons utf-8 handling in mind though. You might want to set the output encoding to utf-8 due to that.

3add3287 avatar Jan 29 '24 13:01 3add3287

I have this problem, too. Fedora 40, Gnome 46, Python 3.11 in virtual environment

SoundDesignerToBe avatar May 29 '24 08:05 SoundDesignerToBe