Skip to content

Can't print bytes with non-ascii encoding #192

Closed
@linuxlc

Description

@linuxlc

example:

print(tabulate([["你好".encode("utf-8"), "你好"]], headers=["bytes", "str"]))

Currently, an UnicodeDecodeError will be raised:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)

Related code is here:

except TypeError:

As I understand it, an UnicodeDecodeError should be caught instead of a TypeError, right?
So output would be like:

bytes                        str
---------------------------  -----
b'\xe4\xbd\xa0\xe5\xa5\xbd'  你好

I'm not sure if it's the designed behavior, but i think it's a proper way to handle non-ascii bytes.
Furthermore, why not return all bytes as str(val) directly without trying to return str(val, "ascii") at first? If the caller want to print decoded value, they can decode it explicitly before calling tabulate().

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions