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

Better display of Chinese #49

Closed
wuhuanyan opened this issue Jun 16, 2020 · 17 comments
Closed

Better display of Chinese #49

wuhuanyan opened this issue Jun 16, 2020 · 17 comments

Comments

@wuhuanyan
Copy link

wuhuanyan commented Jun 16, 2020

The display width of one Chinese character is not equal to the display width of two space characters. I recommend using chr (12288) as a filler for Chinese content.

print('[中文]')
print(f"[{chr(12288)}{chr(12288)}]")
print(f"[{' ' * 4}]")
[中文]
[  ]
[    ]
@wuhuanyan
Copy link
Author

There seems to be no way to achieve it, so I closed it

@wuhuanyan wuhuanyan reopened this Jun 17, 2020
@wuhuanyan
Copy link
Author

wuhuanyan commented Jun 17, 2020

If it is compatible with Chinese, alphanumeric characters need to be converted into full angle characters, and the calculated character length is 1 size. Fill the left and right sides with spaces (or lpad or rpad) with chr(12288). Finally, use "-" as the line divider.

+------+----------+------+------+------------+------+---------+------+-----------+---------+---------+---------+----+
| 会计月份 | 凭证日期     | 凭证种类 | 凭证编号 | 会计科目编码     | 辅助类型 | 辅助编码    | 辅助名称 | 摘要        |    借方金额 |    贷方金额 |      余额 | 方向 |
+------+----------+------+------+------------+------+---------+------+-----------+---------+---------+---------+----+
|    1 |          |      |      |            |      |         |      | 期初余额      |     0.0 |     0.0 |       0 |   |
|    1 |          |      |      |            |      |         |      | 本月合计      |       0 |       0 |       0 |   |
|    1 |          |      |      |            |      |         |      | 本年累计      |       0 |       0 |       0 |   |
|    2 | 20190228 | 记账   | 11   | 1001010201 | 功能科目 | 2013201 | 行政运行 | 提取备用金     | 10000.0 |     0.0 | 10000.0 |   |
|    2 | 20190228 | 记账   | 35   | 1001010201 | 功能科目 | 2013201 | 行政运行 | 何某某住院慰问金  |     0.0 |  2000.0 |  8000.0 |   |
|    2 | 20190228 | 记账   | 35   | 1001010201 | 功能科目 | 2013201 | 行政运行 | 陈某配偶住院慰问金 |     0.0 |  2000.0 |  6000.0 |   |
|    2 | 20190228 | 记账   | 35   | 1001010201 | 功能科目 | 2013201 | 行政运行 | 林某某住院慰问金  |     0.0 |  2000.0 |  4000.0 |   |
|    2 |          |      |      |            |      |         |      | 本月合计      | 10000.0 |  6000.0 |  4000.0 |   |
|    2 |          |      |      |            |      |         |      | 本年累计      | 10000.0 |  6000.0 |  4000.0 |   |
|    3 |          |      |      |            |      |         |      | 本月合计      |     0.0 |     0.0 |  4000.0 |   |
|    3 |          |      |      |            |      |         |      | 本年累计      | 10000.0 |  6000.0 |  4000.0 |   |
|    4 | 20190430 | 记账   | 58   | 1001010201 | 功能科目 | 2013201 | 行政运行 | 张某某住院慰问金  |     0.0 |  1500.0 |  2500.0 |   |
|    4 | 20190430 | 记账   | 58   | 1001010201 | 功能科目 | 2013201 | 行政运行 | 张某某去世慰问金  |     0.0 |  3000.0 |   500.0 |   |
|    4 |          |      |      |            |      |         |      | 本月合计      |     0.0 |  4500.0 |   500.0 |   |
|    4 |          |      |      |            |      |         |      | 本年累计      | 10000.0 | 10500.0 |   500.0 |   |
|    5 | 20190531 | 记账   | 61   | 1001010201 | 功能科目 | 2013201 | 行政运行 | 老干大学学习费   |     0.0 |   100.0 |   600.0 |   |
|    5 | 20190531 | 记账   | 62   | 1001010201 | 功能科目 | 2013201 | 行政运行 | 临时饭卡充值    |     0.0 |   100.0 |   700.0 |   |
|    5 |          |      |      |            |      |         |      | 本月合计      |     0.0 |   200.0 |   700.0 |   |
|    5 |          |      |      |            |      |         |      | 本年累计      | 10000.0 | 10700.0 |   700.0 |   |
|    6 | 20190630 | 记账   | 42   | 1001010201 | 功能科目 | 2013201 | 行政运行 | 高某某住院慰问金  |     0.0 |  2000.0 |  2700.0 |   |
|    6 | 20190630 | 记账   | 44   | 1001010201 | 功能科目 | 2013201 | 行政运行 | 何某某住院慰问金  |     0.0 |  2000.0 |  4700.0 |   |
|    6 | 20190630 | 记账   | 45   | 1001010201 | 功能科目 | 2013201 | 行政运行 | 潘某住院慰问金   |     0.0 |  2000.0 |  6700.0 |   |
|    6 | 20190630 | 记账   | 46   | 1001010201 | 功能科目 | 2013201 | 行政运行 | 王某某住院慰问金  |     0.0 |  1500.0 |  8200.0 |   |
|    6 |          |      |      |            |      |         |      | 本月合计      |     0.0 |  7500.0 |  8200.0 |   |
|    6 |          |      |      |            |      |         |      | 本年累计      | 10000.0 | 18200.0 |  8200.0 |   |
|    7 |          |      |      |            |      |         |      | 本月合计      |     0.0 |     0.0 |  8200.0 |   |
|    7 |          |      |      |            |      |         |      | 本年累计      | 10000.0 | 18200.0 |  8200.0 |   |
|    8 |          |      |      |            |      |         |      | 本月合计      |     0.0 |     0.0 |  8200.0 |   |
|    8 |          |      |      |            |      |         |      | 本年累计      | 10000.0 | 18200.0 |  8200.0 |   |
+------+----------+------+------+------------+------+---------+------+-----------+---------+---------+---------+----+

@hugovk
Copy link
Member

hugovk commented Oct 4, 2020

Please could you provide a minimal code snippet including PrettyTable that reproduces it?

Does it happen with latest master? It sounds like #56 may have helped.

@hugovk
Copy link
Member

hugovk commented Oct 4, 2020

master has just been released as 1.0.0, please can you re-test with that?

https://pypi.org/project/prettytable/1.0.0/

@wuhuanyan
Copy link
Author

wuhuanyan commented Oct 9, 2020

master has just been released as 1.0.0, please can you re-test with that?

https://pypi.org/project/prettytable/1.0.0/

from prettytable import PrettyTable

x = PrettyTable()
x.field_names = ["城市名称", "城市名称编码", "人员", "人均工资(元)"]
x.add_row(["广州", 1295, 1158259, 600.5])
x.add_row(["石家庄", 5905, 1857594, 1146.4])
x.add_row(["加利福利亚", 112, 120900, 1714.7])
x.add_row(["呼和浩特", 1357, 205556, 619.5])

print(x)

released as 1.0.0

+------------+--------------+---------+--------------+
|  城市名称  | 城市名称编码 |   人员  | 人均工资(元) |
+------------+--------------+---------+--------------+
|    广州    |     1295     | 1158259 |    600.5     |
|   石家庄   |     5905     | 1857594 |    1146.4    |
| 加利福利亚 |     112      |  120900 |    1714.7    |
|  呼和浩特  |     1357     |  205556 |    619.5     |
+------------+--------------+---------+--------------+

@hugovk
Copy link
Member

hugovk commented Oct 9, 2020

I get the same (with both PrettyTable 0.7.2 and 1.0.0), it looks good for me in the terminal (iTerm2):

image

But not in GitHub monospace:

+------------+--------------+---------+--------------+
|  城市名称  | 城市名称编码 |   人员  | 人均工资(元) |
+------------+--------------+---------+--------------+
|    广州    |     1295     | 1158259 |    600.5     |
|   石家庄   |     5905     | 1857594 |    1146.4    |
| 加利福利亚 |     112      |  120900 |    1714.7    |
|  呼和浩特  |     1357     |  205556 |    619.5     |
+------------+--------------+---------+--------------+

@wuhuanyan
Copy link
Author

Thank you for your quick reply.
I tested the output of the command window in Python 3.6.8, pretty table 1.0.0 environment, which is a perfect display format.
So the problem is that it appears in the "pycharm" run window.

In addition, I noticed that every Chinese character in the command window has extra display width, but not under "pycharm", which should be the cause of the problem.

1602238922258

@hugovk
Copy link
Member

hugovk commented Oct 9, 2020

Hmm, my PyCharm looks okay.

I added some extra debug:

import os
import sys
import prettytable
print(sys.version)
print(prettytable.__version__)
print(os.environ["TERM"])

iTerm2

image

PyCharm

image

@wuhuanyan
Copy link
Author

command window

image

PyCharm

image

vi ~/.bash_profile
export TERM="xterm-256color"
source ~/.bash_profile

image

@hugovk
Copy link
Member

hugovk commented Oct 10, 2020

@vrza Hi! Do you by any chance know what's up here?

@wuhuanyan
Copy link
Author

I think my pycharm settings are different from yours.
Can I see your settings?
pycharm->preferences->Editor->Color Scheme->Console Font

@hugovk
Copy link
Member

hugovk commented Oct 10, 2020

I have JetBrains Mono:

image

@vrza
Copy link
Contributor

vrza commented Oct 13, 2020

Seems to work fine for me with JetBrains Mono in PyCharm on Linux

pycharm-jetbrains-mono

@vrza
Copy link
Contributor

vrza commented Oct 13, 2020

In some other applications, like Gedit, Kate and Slack on Linux (pictured below), however, it is problematic.

slack-prettytable

Chinese characters that should be rendered to be exactly 2 "cells" wide (where cell is the width of a single monospace font character) are rendered with width < 2.

Vim, Emacs and most terminal emulators seem to render it nicely.

On first glance, the issue seems to be in the application that's rendering the text or the rendering engine it uses.

This is not a bug in prettytable or the wcwidth library. In the terminal we can only work with whole cells, and we rely on the application (e.g. terminal emulator) to align all characters to integer multiples of 1 cell.

@vrza
Copy link
Contributor

vrza commented Oct 13, 2020

It seems like quite a complex issue as some character widths when printed in monospaced font are not standardized. This blog post may have some useful information (do notice how many times it was edited): https://denisbider.blogspot.com/2015/09/when-monospace-fonts-arent-unicode.html

@onion1003
Copy link

onion1003 commented Mar 31, 2021

solution
choose “Emulate terminal in output console” in pycharm will solve the display problem in pycharm.

For pandas, you also need to add

pd.set_option('display.unicode.ambiguous_as_wide', True)
pd.set_option('display.unicode.east_asian_width', True)

@swfcsh
Copy link

swfcsh commented Jun 11, 2021

solution
choose “Emulate terminal in output console” in pycharm will solve the display problem in pycharm.

For pandas, you also need to add

pd.set_option('display.unicode.ambiguous_as_wide', True)
pd.set_option('display.unicode.east_asian_width', True)

It works fine for me!
Thanks!

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

5 participants