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

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("------------------------------------------------------------------------------------")

最佳答案

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

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")

关于python - 奇怪的打印语法错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45497857/

10-12 23:12