开发者

Pretty Print Formatting Issue in Python

 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), "+"

I have the following code in place to try and print out the results of a db query. In a standalone file, it prints the following output:

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

When placed 开发者_开发知识库in a larger file within a function, it does not work however. We get the following error:

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

Help?


Python 2.6 does not support zero-length field names in format strings.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜