【问题标题】:Give different format to output of SQLite为 SQLite 的输出提供不同的格式
【发布时间】:2020-02-19 12:44:44
【问题描述】:

我有一个这样的代码来从数据库中输出数据,其中首先是用户的名字,然后是用户的生日年份

name = db.execute(f'SELECT first, birth FROM mydatabase WHERE house = "{house_roster}"') 
print(name[0])

我第一行的输出 (print[0]) 是这样的:

{'first': 'Cedric', 'birth': 1977}

我怎样才能让它变成这样?

Cedric, 1977

【问题讨论】:

  • print( "{}, {}".format( name[0]["first"], name[0]["birth"] )) 如果你不需要逗号,那么即使是print( name[0]["first"], name[0]["birth"] )
  • 首选:db.execute('SELECT first, birth FROM mydatabase WHERE house = ?', (house_roster, ))。更安全。
  • 这非常有用,谢谢!

标签: python sqlite cs50


【解决方案1】:
print(', '.join(name[0].values()))

这是一个通用的解决方案,适用于任意数量和任意名称的字段。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-09
    • 2016-04-05
    • 2021-10-16
    • 2017-03-15
    相关资源
    最近更新 更多