-1

我想在表格中输出从 API 获得的列表,这样看起来更好。来自 API 的数据通过 for 循环返回。我现在的问题是我不知道如何让标题每次都没有循环。

例如:

我想要一个标题,其余的在标题下

我已经从这里得到了一些说明,数据将在表格中完全显示为“制表”。但是,“标题”将在每次运行时再次插入。

head = ["Datum", "Username", "License Server", "Feature Name", "Max. Usage", "Hours Used", "Max Used", "Hours Borrowed", "Max Borrowed"]

for item in (data['data'])
   if item['un'] == tecNo:
      print(tabulate([[item['fud'], item['un'], str(item['lsn']), str(item['fns']), str(item['musage']), str(item['hu']), str(item['mu']), str(item['hb']), str(item['mb'])]],headers=head, tablefmt="grid"))```


  [1]: https://i.stack.imgur.com/ZHODf.png
  [2]: https://i.stack.imgur.com/pNci2.png
4

1 回答 1

0

您需要将项目放在二维数组中,然后tabulate在最后调用。

table = []
for item in (data['data'])
   if item['un'] == tecNo:
      table.append([
          item['fud'], 
          item['un'], 
          str(item['lsn']), 
          str(item['fns']), 
          str(item['musage']), 
          str(item['hu']), 
          str(item['mu']), 
          str(item['hb']), 
          str(item['mb'])
      ])
tabulate(table, headers=head)
于 2019-07-18T16:02:11.370 回答