奇怪的打印语法错误

时间:2017-08-04 03:35:49

标签: python syntax-error

我的print语句周围有一个括号和副词,它看起来和我的其他print语句相同,但是我收到语法错误。怎么了? (这是此代码块中的最终打印语句)

def printSummary(itemRecords) :
    # printSummary prints a summary from the itemRecords dictionary
    print("ID     NAME     Start Available     Reservations     Subtractions     End Stock")
    print("---     -----   --------------       ----------       -----------      ---------")
    for rec in itemRecords.values():
        print("{0:5s} {1:12s} {2:6d} {3:6d} {4:6d} {5:6d}".format(rec.getID(), rec.getName(), rec.getAvailableStart(), rec.getReservations(), rec.getCancellations(), rec.getAvailableEnd())
    #print the footer
    print("------------------------------------------------------------------------------------")

2 个答案:

答案 0 :(得分:1)

以下是您问题的替代解决方案:

from tabulate import tabulate

def printSummary(itemRecords) :
    result = list()
    headers = ["ID", "NAME", "Start Available", "Reservations", "Subtractions", "End Stock"]
    result.append(headers)

    for rec in itemRecords.values():
        row_rec = list()
        row_rec.append([rec.getID(), rec.getName(), rec.getAvailableStart(), rec.getReservations(), rec.getCancellations(), rec.getAvailableEnd()])
        result.append(row_rec)


    print tabulate(result, headers="firstrow")

答案 1 :(得分:1)

请检查此行:您缺少参数。

错:

print("{0:5s} {1:12s} {2:6d} {3:6d} {4:6d} {5:6d}".format(rec.getID(), rec.getName(), rec.getAvailableStart(), rec.getReservations(), rec.getCancellations(), rec.getAvailableEnd()) 

右:

print("{0:5s} {1:12s} {2:6d} {3:6d} {4:6d} {5:6d}".format(rec.getID(), rec.getName(), rec.getAvailableStart(), rec.getReservations(), rec.getCancellations(), rec.getAvailableEnd()))