Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
linuxlc opened this issue Aug 11, 2022 · 2 comments
Closed

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

linuxlc opened this issue Aug 11, 2022 · 2 comments

Comments

@linuxlc
Copy link

linuxlc commented Aug 11, 2022

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

@FurcyPin
Copy link
Contributor

FurcyPin commented Sep 4, 2022

+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 ?

FurcyPin added a commit to FurcyPin/bigquery-frame that referenced this issue Sep 4, 2022
from_base32 and from_base64 will be added later, as the tabulate
library suffers from a display bug at the moment.
astanin/python-tabulate#192
FurcyPin added a commit to FurcyPin/python-tabulate that referenced this issue Sep 5, 2022
FurcyPin added a commit to FurcyPin/bigquery-frame that referenced this issue Sep 5, 2022
from_base32 and from_base64 will be added later, as the tabulate
library suffers from a display bug at the moment.
astanin/python-tabulate#192
@FurcyPin
Copy link
Contributor

FurcyPin commented Sep 5, 2022

I made a pull request to fix the issue.

FurcyPin added a commit to FurcyPin/bigquery-frame that referenced this issue Sep 13, 2022
from_base32 and from_base64 will be added later, as the tabulate
library suffers from a display bug at the moment.
astanin/python-tabulate#192
@astanin astanin closed this as completed in 1707234 Oct 6, 2022
astanin added a commit that referenced this issue Oct 6, 2022
fix #192 - Can't print bytes with non-ascii encoding
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants