1

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
Arjun Singh
  • 21
  • 1
  • 7

2 Answers2

0

This is most likely a problem with your Windows terminal and not with your code.

Try setting chcp 65001 in your console, which changes the code page to UTF-8. print("├") works for me after that.

https://technet.microsoft.com/en-us/library/bb490874.aspx

orangeInk
  • 1,360
  • 8
  • 16
0

I was able to get the expected output by using codecs. This answer helped me. Using codec worked.

tree_structure = codecs.escape_decode(bytes(tree_structure, "utf-8"))[0].decode("utf-8")
Arjun Singh
  • 21
  • 1
  • 7