简体   繁体   English

Python,多次打印内容

[英]Python, printing things a select number of times

I'm new to coding and am having an issue with my program. 我是编码新手,我的程序有问题。 I have to get sales information from a file and print it in a certain format. 我必须从文件中获取销售信息并以某种格式打印。 This is the code: 这是代码:

#Looping program to read file per line
for line in lines:
    # Formatting line to read later
    line = line.strip().split(',')
    year = line[0]
    year = int(year)
    month = line[1]
    month = int(month)
    day = line[2]
    day = int(day)
    linePoint = date(year, month, day)

    cost = line[3]
    cost = float(cost)

    #Finding the lines within the start and end date
    if (linePoint >= startDate) and (linePoint <= endDate):
        printcost = (cost / 100)
        printcost = int(printcost)

        print "%s.%s.%s" % (startYear, startMonth, startDay)
        print "0%s:" % printNum,  "*" * printcost

        newcost = newcost + cost
        printNum += 1

When I use the %s.%s.%s it's printing the date above the sales, I want it to print that above the other print statement once per month, and be able to increase it once the month is up. 当我使用%s.%s.%s它会在销售上方打印日期,我希望它每个月在其他打印对帐单上打印一次,并能在每月结束时增加它。 Also in the print "0%s:" % printNum, "*" * printcost statement I would like it to only print the zero for the first nine days. 同样在print "0%s:" % printNum, "*" * printcost语句中,我希望它仅在前九天打印零。

Essentially my question is how in Python do I run something a certain number of times, but the number of times is dependent on the user and correlate with the date, and in order to do that the computer needs to be able to recognize the date. 本质上,我的问题是我如何在Python中运行一定次数,但是次数取决于用户并与日期相关,因此计算机必须能够识别日期。 Sorry for the vagueness. 抱歉,含糊不清。

If you want the output to be '01', '02', ..., '10', '11', ... then the format you want to use is: 如果希望输出为'01', '02', ..., '10', '11', ... ,则要使用的格式为:

print "%02d" % printNum

As for printing the header out at the start of each new month (that's how I'm reading the first part of your question, you could do something like: 至于在每个新的月初打印标题(这就是我阅读问题的第一部分的方式),您可以执行以下操作:

old_month = 0
for line in lines:
    # do stuff
    month = whatever...
    if month != old_month:
        # print header here
        old_month = month
    #rest of loop

I'm almost sure this is what you want. 我几乎可以确定这就是您想要的。 Note the "%02d" format specifier , which gives you the leading zero, and the check to see if the month has changed via if month != current_month . 请注意“%02d” 格式说明符 ,该说明符为您提供前导零,并通过if month != current_month来检查月份是否已更改。

current_month, print_num, new_cost = None, 0, 0

for line in lines:
    fields = line.strip().split(',')
    year = int(fields[0])
    month = int(fields[1])
    day = int(fields[2])
    cost = float(fields[3])

    line_date = date(year, month, day)

    #Finding the lines within the start and end date
    if startDate <= line_date <= endDate:
        if month != current_month:
            print "%s.%s.%s" % (year, month, day)
            current_month = month

        print_cost = int(cost / 100)
        print "%02d: %s" % (print_num,  "*" * print_cost)

        new_cost += cost
        print_num += 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM