简体   繁体   English

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

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

Currently, a Python beginner looking for some help. 目前,一位Python初学者正在寻求帮助。 I'm reading from a text file with 365 lines of integers. 我正在从具有365行整数的文本文件中读取。 Each integer is representative of a day of the year. 每个整数代表一年中的一天。 Like this, but for 365 lines: 像这样,但对于365行:

1102
9236
10643
2376
6815
10394
3055
3750
4181
5452
10745

I need to go through the whole file and separate the 365 days into each of the 12 months and take the average of the numbers of each month. 我需要遍历整个文件,并将365天分成12个月中的每个月,并取每个月的平均值。 For instance, the first 31 lines are January, take the average, print it, then continue from there... 例如,前31行是一月,取平均值,打印出来,然后从那里继续...

At this point I have written code that goes through the whole file and gives a total for the year and an average per day, but am stuck on splitting the file into the separate months and taking individual averages. 至此,我编写了遍历整个文件的代码,给出了一年的总和和每天的平均值,但是仍然坚持将文件拆分为单独的月份并取各个平均值。 What do I do to achieve this? 我该怎么做?

Here's my current code: 这是我当前的代码:

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

I hope this question was clear enough. 我希望这个问题足够清楚。 Thanks for any help! 谢谢你的帮助!

You'll have to have the number of days per month. 您必须拥有每月的天数。 Either use a fixed table, or you could ask the calendar module: 您可以使用固定表,也可以询问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]

In case you decide to use a fixed table the only month of interest is February during leap years. 如果您决定使用固定表,则leap年中的唯一月份是2月。 You could just increment that if calendar.isleap() is True. 如果calendar.isleap()为True,则可以增加该值。

Given an open file of integer per row you could simply slice it suitably, map int() over the slices, and use statistics.mean() : 给定一个打开的每行整数文件,您可以简单地对其进行适当切片 ,将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]

where the_file was simply 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

First, you need a list with the quantity of days in each month: 首先,您需要一个列表,其中包含每月的天数:

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

Now write a loop to step through the months, with another inside to step through the days: 现在编写一个循环来逐步浏览月份,并编写另一个循环来逐步浏览日子:

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

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

Do remember that Python indexing starts at 0, so you'll have month_num running 0-11, and day_num running 0-30 at most. 请记住,Python索引从0开始,因此您将使month_num运行0-11,并且day_num最多运行0-30。

Can you take it from there? 你能从那里拿走吗?


RESPONSE TO OP'S COMMENT 对OP的评论的回应

Okay, you're handicapped: lists are disallowed. 好的,您很残障:不允许使用列表。 Instead, try this: 相反,请尝试以下操作:

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