繁体   English   中英

如何在Python中遍历文本文件的某些部分?

[英]How do I loop through certain parts of a text file in Python?

目前,一位Python初学者正在寻求帮助。 我正在从具有365行整数的文本文件中读取。 每个整数代表一年中的一天。 像这样,但对于365行:

1102
9236
10643
2376
6815
10394
3055
3750
4181
5452
10745

我需要遍历整个文件,并将365天分成12个月中的每个月,并取每个月的平均值。 例如,前31行是一月,取平均值,打印出来,然后从那里继续...

至此,我编写了遍历整个文件的代码,给出了一年的总和和每天的平均值,但是仍然坚持将文件拆分为单独的月份并取各个平均值。 我该怎么做?

这是我当前的代码:

import math

def stepCounter ():
    stepsFile = open("steps.txt", "r")
    stepsFile.readline()

    count = 0
    for line in stepsFile:
        steps = int(line)
        count = count + steps
        avg = count / 365
    print(count, "steps taken this year!")
    print("That's about", round(avg), "steps each day!")

  stepsFile.close()

stepCounter()

我希望这个问题足够清楚。 谢谢你的帮助!

您必须拥有每月的天数。 您可以使用固定表,也可以询问calendar模块:

In [11]: months = [calendar.monthrange(2017, m)[1] for m in range(1, 13)]

In [12]: months
Out[12]: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

如果您决定使用固定表,则leap年中的唯一月份是2月。 如果calendar.isleap()为True,则可以增加该值。

给定一个打开的每行整数文件,您可以简单地对其进行适当切片 ,将int()映射到切片上,然后使用statistics.mean()

In [17]: from statistics import mean

In [18]: from itertools import islice

In [19]: [mean(map(int, islice(the_file, mdays))) for mdays in months]
Out[19]: [15, 44.5, 74, 104.5, 135, 165.5, 196, 227, 257.5, 288, 318.5, 349]

the_file只是

In [13]: from io import StringIO

In [14]: the_file = StringIO()

In [15]: the_file.writelines(map('{}\n'.format, range(365)))

In [16]: the_file.seek(0)
Out[16]: 0

首先,您需要一个列表,其中包含每月的天数:

month_len = [31, 28, 31,
             30, 31, 30,
             31, 31, 30,
             31, 30, 31]

现在编写一个循环来逐步浏览月份,并编写另一个循环来逐步浏览日子:

for month_num in range(12):
    # Start a new month

    for day_num in range(month_len[month_num]):
        #Process one day

请记住,Python索引从0开始,因此您将使month_num运行0-11,并且day_num最多运行0-30。

你能从那里拿走吗?


对OP的评论的回应

好的,您很残障:不允许使用列表。 相反,请尝试以下操作:

for month_num in range(1, 12):
    month_len = 31
    if month_num = 2:
        month_len = 28
    elif month_num = 4 or
         month_num = 6 or
         month_num = 9 or
         month_num = 11:
        month_len = 30

    for day_num in range(month_len):

暂无
暂无

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

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