Python - 从导入的文件中读取时循环打印次数过多

时间:2018-04-16 00:51:53

标签: python list loops

我正在编写程序来输出计算工资单的表格。 它是从导入的文件中读取的。

我用来测试的文件包含:

  • 倒钩,Moran的,51,12.85
  • 乐,Rinehimer,30,9.35
  • 乔,贝鲁奇,45,9.55
  • 戴夫,福莱姆,37,17.70

这里的想法是取每条线并将其拆分,然后按位置按位置打印我需要的东西。 以下是我认为包含该问题的代码部分:

def getFile():
    filename = input("Please enter the employee file name: ")
    print()
    infile = open(filename, "r")
    return infile

def outputs(infile):
    paylist = []
    count = 0

    for lines in infile:
        individual = lines.split(",")
        for pos in individual:
            first = ("{0:7}".format (individual[0]))
            print(first, end = "")

            last = ("{0:12}".format(individual[1]))
            print(last, end = "")

            hours = ("{0:6}".format(individual[2]))
            print(hours, end = "")

            wage = ("{0:6}".format(individual[3]))
            print ("$",wage, end = "")

当我运行它时,它输出所有正确的值,但是输出

4次

1 个答案:

答案 0 :(得分:0)

outputs()的内环是罪魁祸首。删除它。

def outputs(infile):
    paylist = []
    count = 0
    for lines in infile:
        individual = lines.split(",")
        first = ("{0:7}".format (individual[0]))
        print(first, end = "")

        last = ("{0:12}".format(individual[1]))
        print(last, end = "")

        hours = ("{0:6}".format(individual[2]))
        print(hours, end = "")

        wage = ("{0:6}".format(individual[3]))
        print ("$",wage, end = "")