1

I used PrettyTable module in the Python 3.6,but the output table is not aligned (like the red rectangle in the picture).I have searched this issue on the internet,but no good answer.

Is it because there're some Chinese language in the table? enter image description here

All the data of the rows are from a list which contains lots of dictionaries. The code is as follows:

 def prettyPrint(self,trains):
        pt = PrettyTable()
        pt.title = 'Results for Query Tickets'
        pt.field_names = ["车次", "出发站","到达站", "出发时间","到达时间", "历时", "商务座特等座","一等座", "二等座", "软卧", "动卧", "硬卧", "硬座", "无座"]
        pt.align["车次"] = "l"
        for train in trains:
            pt.add_row(train.values())
        print(pt) 

How to make it? Thanks.

Ringo
  • 1,173
  • 1
  • 12
  • 25

2 Answers2

0

It looks like none of the first digits of your values are aligned straight, leading your '|' symbols to go out of line as a result. A fix is to make sure the spacing is correct while listing values for the program to print. If the values are not the same length, then add spaces.

Cachario
  • 41
  • 1
  • 1
  • 6
  • Yes, the length of the values are not the same.I will try to add some spaces for the value and try to print again. – Ringo Aug 07 '18 at 03:04
  • I have known the reason.We need to run the project in the terminal instead of IDE. – Ringo Aug 07 '18 at 06:23
0

Your console is using mixed fonts. The font for the western characters appears monospaced but the Chinese characters do not align to the typical double width cell grid. That suggests that the primary monospace font lacks Chinese characters and a substitute font was selected for the Chinese. The substitute has different metrics for character advance that don't align with the monospaced western font.

Kevin Thibedeau
  • 3,299
  • 15
  • 26