Python 中的漂亮打印格式问题

发布于 2024-10-19 06:53:28 字数 1200 浏览 2 评论 0原文

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栖迟 2024-10-26 06:53:28

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

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

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

print "+", '{0:^90}'.format(''), "+"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文