如何使用一个标题打印表

时间:2015-12-03 12:15:44

标签: python python-3.x

someFile = open("komad_namestaja.txt", "r")

for linija in someFile.readlines():
    code, name, color, quantity, price, category = linija.split("|")

    print("---------------------------------------------------------------------+")
    print("Code   |Name         |Color      |Quantity|Price       |category   |")    #Header     print("-------+-------------+-----------+--------+-----------+-------------+")
    print("{0:8.8}|{1:14.14}|{2:10.10}|{3:8.8}|{4:11.11}|{5:13.6}|".format(code, name, color, quantity, price, category))

这是我从文件制作信息表的代码。 问题是,代码打印文件新标题中的每一行。 怎么了?

1 个答案:

答案 0 :(得分:1)

只需在循环之前移动标题,然后在循环中打印信息:

someFile = open("komad_namestaja.txt", "r")

print("-------------------------------------------------------------------+")
print("Code   |Name         |Color      |Quantity|Price       |category   |")       
print("-------+-------------+-----------+--------+------------+-----------+")

for linija in someFile.readlines():
    code, name, color, quantity, price, category = linija.split("|")
    print("{0:8.8}|{1:14.14}|{2:10.10}|{3:8.8}|{4:11.11}|{5:13.6}|".format(code, name, color, quantity, price, category))