jazzband / prettytable

Display tabular data in a visually appealing ASCII table format
https://pypi.org/project/PrettyTable/
Other
1.35k stars 154 forks source link

Does not align when there is Chinese #213

Closed douloveppt closed 1 year ago

douloveppt commented 1 year ago

This is my code

table = pt.PrettyTable(["设备名称", "iRules名称", "阶段", "内容", "次数"])
# table = pt.PrettyTable(["device", "iRules", "stage", "content", "count"])
table.align["阶段"] = "l"
table.align["内容"] = "l"
for item, count in data.items():
    row = [self.obj, irules, item[0], item[1], count]
    table.add_row(row)
print(table)

output:

+--------------------------+-----------------------+--------------+-----------------------------------------------------------+------+
|         设备名称         |       iRules名称      | 阶段         | 内容                                                      | 次数 |
+--------------------------+-----------------------+--------------+-----------------------------------------------------------+------+
| bigip01_15.1.3_60.13.com | irules_ncmps_hslhttp  | HTTP_REQUEST | ERR_NOT_SUPPORTED (line 1) invoked from within HTTP::host |  4   |
| bigip01_15.1.3_60.13.com | irules_ncmps_hslhttp  | RULE_INIT    | can't read b: no such variable while executing set a      |  5   |
+--------------------------+-----------------------+--------------+-----------------------------------------------------------+------+

by the way, how do I make the content left aligned but the header center aligned?

thanks!!!

firai commented 1 year ago

Seems like a duplicate of #49. Your browser or program is most likely substituting a different font for the Chinese characters, because the monospace font being used doesn't have Chinese glyphs. This results in the width of (substituted) Chinese font not being exactly 2 times that of the Latin glyphs. The table is aligned if a monospace font that has Chinese characters is used, such as Sarasa Gothic. The term SC regular variant is shown below.

image