caesar0301 / treelib

An efficient implementation of tree data structure in python 2/3.
http://treelib.readthedocs.io/en/latest/
Other
800 stars 185 forks source link

tree.show prints binary literal to stdout #221

Open aholten opened 7 months ago

aholten commented 7 months ago

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'

Lab-Brat commented 7 months ago

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.

sylock commented 5 months ago

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

3add3287 commented 5 months ago

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.

SoundDesignerToBe commented 1 month ago

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