The output of code varies on Linux and Windows.I am not much familiar with encodings, which is what this problem seems to involve.
Here's my code:
import sys
from treelib import Tree
from io import StringIO
# creating and populating 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")
# var to store standard output
output = StringIO()
sys.stdout = output
tree.show()
# restoring standard output to console
sys.stdout = sys.__stdout__
tree_structure = output.getvalue()
print(tree_structure)
I am getting expected output on Linux but on Windows result have chars encoded as \xNN.
Linux output:
Harry
├── Bill
└── Jane
├── Diane
│ └── Mary
└── Mark
Windows Output:
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'
Though the result of sys.stdout.encoding
was 'utf-8'
both on windows and linux.
The closest I could reach to the expected output was by adding following before print statement:
#removing b'' from string
tree_structure = tree_structure[2:-2]
# converting to bytes
tree_structure = bytes(tree_structure,'utf-8')
tree_structure = tree_structure.decode('unicode_escape')
print(tree_structure)
output afterward:
Harry
âââ Bill
âââ Jane
âââ Diane
â âââ Mary
âââ Mark