开发者

Pretty Print Formatting Issue in Python

开发者 https://www.devze.com 2023-02-13 04:46 出处:网络
str = \"\" for i in range(1,91): str = str + \'-\' print \"+\", \'{:^90}\'.format(str), \"+\" for elem in cursor:
 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

精彩评论

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