Skip to content

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

Closed

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().

Activity

  1. FurcyPin commented on Sep 4, 2022

    @FurcyPin
    Contributor

    +1
    Ran into the same issue, and replacing TypeError with UnicodeDecodeError works in my case too.
    Perhaps we should catch both type of errors, though ?

  2. added 4 commits that reference this issue on Sep 4, 2022
  3. FurcyPin commented on Sep 5, 2022

    @FurcyPin
    Contributor

    I made a pull request to fix the issue.

  4. added a commit that references this issue on Oct 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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