themaestro
themaestro

Reputation: 14256

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?

Upvotes: 2

Views: 1356

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798536

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

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

Upvotes: 6

Related Questions