Python中的漂亮打印格式问题

时间:2011-02-23 19:29:45

标签: python pretty-print

 str = ""
 for i in range(1,91):
     str = str + '-'

 print "+", '{:^90}'.format(str), "+"
 for elem in cursor:
     print "|", '{:^8}'.format(elem['classid']), \
           "|", '{:^8}'.format(elem['dept']), \
           "|", '{:^8}'.format(elem['coursenum']), \
           "|", '{:^8}'.format(elem['area']), \
           "|", '{:<46}'.format(elem['title']), \
          "|"
 print "+", '{:^90}'.format(str), "+"

我有以下代码来尝试打印出db查询的结果。在独立文件中,它打印以下输出:

+ ------------------------------------------------------------------------------------------ +
| centered | centered | centered | centered | 12                                             |
| centered | centered | centered | centered | 12                                             |
| centered | centered | centered | centered | 12                                             |
+ ------------------------------------------------------------------------------------------ +

当放置在函数中的较大文件中时,它不起作用。我们收到以下错误:

  File "reg.py", line 58, in printHumanOutput
    print "+", '{:^90}'.format(''), "+"
ValueError: zero length field name in format

帮助?

1 个答案:

答案 0 :(得分:6)

Python 2.6不支持格式字符串中的零长度字段名称。

print "+", '{0:^90}'.format(''), "+"