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), "+"
我有以下代码来尝试打印数据库查询的结果。在独立文件中,它会打印以下输出:
+ ------------------------------------------------------------------------------------------ +
| 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
帮助?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Python 2.6 不支持格式字符串中的零长度字段名称。
Python 2.6 does not support zero-length field names in format strings.