如何在 python 3.8.10 中仅打印时间日志文件(txt 文件)中的开始和结束时间

罗希特

这是我正在使用的代码,我得到了部分输出

def main():
    with open('time.txt') as f:
        linenumber=0
        for lines in f:
            a=f.readlines()
            for i in range(0,len(lines)):
                print(a[i].split()[1],a[i].split()[3])
            #print (a[i])
            


if __name__=='__main__':
    main()

txt 文件由以下元素组成:time.txt

我得到的输出是输出

在输出中,程序直到 txt 文件末尾才打印,它只打印到某一行并停止。不知道原因。

腰带辛哈

假设您有一个名为VALIDtime.txt的文件,其中包含以下内容

Time Log:
2/23/12: 9:10pm - 11:40pm getting familiar with Flash
2/29/12: 12:50pm - 2:00pm getting familiar with Flash
3/1/12: 6:00pm - 11:40pm getting familiar with Flash
3/3/12: 3:00pm - 7:00pm step-debug Energy Game code
3/4/12: 8:00pm - 11:40pm start carbon game
3/5/12: 2:00pm - 3:00pm
        4:00pm - 4:30pm carbon game
3/6/12: 11:30am - 1:30pm carbon game data structures and classes for the first action
3/7/12: 11:00am - 5:00pm tested basic concept
3/8/12: 1:00am - 1:30am changed vector calling,
        10:30am - 2:30pm
        4:00pm - 5:00pm wrote code to draw points indicator and marshal the data code;
3/9/12: 12:00am - 2:30am added CarbonActionCategoryView.as and captured some icons 
         11:30am - 4:50pm research how to resize; labor work to capture icons; research how to use event/delegate;
3/10/12: 1:50am - 6:00am event/delegate alternative implementation; score fields implementation
         10:40am - 1:00pm define CarbonConsts.as for those names; implemented points fields
3/12/12: 10:45am - 5:00pm research on scrollpane, to no avail
3/13/12: 11:00am - 5:00pm research on Slider customization and make some progress without fully satisfication
3/14/12: 12:10pm - 5:00pm continue to research on Slider and got some idea
3/15/12: 3:30am - 4:30am experiment with CarbonSlider concept
         11:00am - 5:00pm "Flash Training with Paul Trani" and continue to experiment with CarbonSlider concept
3/16/12: 10:00am - 5:00pm integrate the CarbonSlider class into the Game project
         8:00pm - 9:00pm added the display of the slider thumb value; 
         9:30pm - 11:00pm clean-up the resources and adjust slider.x; start to modify CarbonPointsIndicator.as
3/17/12: 6:00am - 7:00am 
         12:00pm - 12:50pm
         4:00pm - 7:00pm bad CarbonPointsIndicator modification
3/18/12: 9:10am - 9:40am draw 2-pixels rectangle and 1-pixel tick for Points Indicator
         9:40am - 10:30am debug biofueled cars & trucks, mix in wood in coal plants
         11:30am - 3:30pm scrollpane implementation - alternative
         11:00pm - 12:00am read Adobe Flash Professional online help
3/19/12: 5:00am - 6:00am read Adobe Flash Professional online help
3/20/12: 9:00am - 10:00am discuss with Dr. Lant and Blanca.
         10:00am - 11:00am slider width will be proportional per page or section, total score is changed to total carbon points, options remaining tops 100
3/23/12: 1:30pm - 3:30pm install Adobe Illustrator
         4:00pm - 5:00pm Flash Professional online help
3/29/12: 3:11am - 7:00am background shape study, to no avail
         11:00am - 12:50pm continue background shape study - no longer needed
         3:00pm - 5:05pm scroll pane automatically adjusted based on the view size; dynamically created static texts
         10:25pm - 11:00pm adjust positions of text fields
3/30/12: 9:50am - 11:30am loading images in AS 3 training; flash work cycle training
         11:40am - 2:20pm experiment with ideas; learn illustrator
         3:20pm - 5:05pm re-captured all .png files; .ai files do not have any advantage over .png files
3/31/12: 7:00am - 9:00am change the color of points indicator; add PPU; use Bitmap instead of MovieClip;
4/2/12: 12:10pm - 1:42pm study mask and Shape; come up with the concept of carbon progressbar 
        1:43pm - 5:00pm implement the carbon progressbar using mask and shape
        7:00pm - 7:40pm implement the carbon progressbar by drawing bar and outline
        11:00pm - 12:05am added total options and options
4/3/12: 1:30am - 3:00am added Options In Total static text field; Options Remaining will not go negative.
4/4/12: 3:50pm - 5:00pm update the icons
        8:35pm - 11:50pm continue to update the icons; use the colors in .ai file
4/9/12: 12:30pm - 6:00pm update based on feedback
        9:50pm - 2:30am
4/10/12: 8:56am - 9:57am
        - 10.remove "other"
        - 9. add "Replace Cement"
        - 8. move current points slightly to the right
        - 7. center max points on last tic mark
        - 6. provide a bit more white space for four large boxes at the top
        - 5. At top of left-hand column, label boxes "Tons of Carbon per unit"
        - 4. PPU boxes. Align vertically. Expand. "Points per Unit" at the top of this column. 
        - 3. decrease Improve Insulation limit from 20 to 15
        - 2. optionsRemaining and optionsUsed
        - 1. total carbon points bar
5/19/12: 4:03pm - 6:57pm update based on teachers' feedback and dynamically dragging slide
6/4/12: 3:30pm - 5:04pm prev/more options implementation
        8:15pm - 10:00pm
3/13/13: 2:11pm - 5:12pm Make numbers in the yellow triangles bigger and bolder; Make numbers in the options used and remaining bigger and bolder

您可以使用辅助函数来检查一行的可能部分是否为"starttime",以amor结尾pm

def is_time(s: str) -> bool:
    return s.endswith('am') or s.endswith('pm')

def main() -> None:
    with open('time.txt', 'r') as log_file:
        next(log_file)  # Skip first line.
        for line in log_file:
            line_parts = line.split()
            if line_parts[0].count('/') == 2:  # Line starts with a date.
                if is_time(line_parts[1]):
                    print(' '.join(line_parts[1:4]))
            else:
                if is_time(line_parts[0]):
                    print(' '.join(line_parts[0:3]))

if __name__ == '__main__':
    main()

输出:

9:10pm - 11:40pm
12:50pm - 2:00pm
6:00pm - 11:40pm
3:00pm - 7:00pm
8:00pm - 11:40pm
2:00pm - 3:00pm
4:00pm - 4:30pm
11:30am - 1:30pm
11:00am - 5:00pm
1:00am - 1:30am
10:30am - 2:30pm
4:00pm - 5:00pm
12:00am - 2:30am
11:30am - 4:50pm
1:50am - 6:00am
10:40am - 1:00pm
10:45am - 5:00pm
11:00am - 5:00pm
12:10pm - 5:00pm
3:30am - 4:30am
11:00am - 5:00pm
10:00am - 5:00pm
8:00pm - 9:00pm
9:30pm - 11:00pm
6:00am - 7:00am
12:00pm - 12:50pm
4:00pm - 7:00pm
9:10am - 9:40am
9:40am - 10:30am
11:30am - 3:30pm
11:00pm - 12:00am
5:00am - 6:00am
9:00am - 10:00am
10:00am - 11:00am
1:30pm - 3:30pm
4:00pm - 5:00pm
3:11am - 7:00am
11:00am - 12:50pm
3:00pm - 5:05pm
10:25pm - 11:00pm
9:50am - 11:30am
11:40am - 2:20pm
3:20pm - 5:05pm
7:00am - 9:00am
12:10pm - 1:42pm
1:43pm - 5:00pm
7:00pm - 7:40pm
11:00pm - 12:05am
1:30am - 3:00am
3:50pm - 5:00pm
8:35pm - 11:50pm
12:30pm - 6:00pm
9:50pm - 2:30am
8:56am - 9:57am
4:03pm - 6:57pm
3:30pm - 5:04pm
8:15pm - 10:00pm
2:11pm - 5:12pm

要计算总小时数,您可以将每个时间间隔解析为timedelta

from datetime import datetime, timedelta

def is_time(s: str) -> bool:
    return s.endswith('am') or s.endswith('pm')

def parse_12_hour_time(time: str) -> datetime:
    return datetime.strptime(time, '%I:%M%p')

def get_timedelta(start_time: str, end_time: str) -> timedelta:
    return parse_12_hour_time(end_time) - parse_12_hour_time(start_time)

def timedelta_to_hours(td: timedelta) -> float:
    return td.total_seconds() / 3600

def main() -> None:
    total_hours = 0
    with open('time.txt', 'r') as log_file:
        next(log_file)  # Skip first row.
        for line in log_file:
            line_parts = line.split()
            if line_parts[0].count('/') == 2:  # Line starts with a date.
                if is_time(line_parts[1]):
                    td = get_timedelta(line_parts[1], line_parts[3])
                    total_hours += timedelta_to_hours(td)
            else:
                if is_time(line_parts[0]):
                    td = get_timedelta(line_parts[0], line_parts[2])
                    total_hours += timedelta_to_hours(td)
    print(f'{total_hours = :.2f}')

if __name__ == '__main__':
    main()

输出:

total_hours = 74.38

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Python 3中以开始时间和结束时间播放MP3文件的最佳方法

Python - 如何在csv中获取带有开始和结束时间列的“持续时间”列?

时间戳python中昨天的开始和结束时间

在python中以不同的开始和结束时间分割wav文件

python程序,找出音频文件中每个单词的开始和结束时间

Python:从CSV文件中的开始时间列和结束时间列获取总小时数

如何在Google Calendar API的事件对象中设置开始时间和结束时间

如何在BaseX中记录任务开始时间和任务结束时间

如何在ASP.NET C#中获取会话的开始和结束时间

如何在ios中获取今天日期的开始和结束时间?

如何在python中的多个文本文件中显示输出,例如:在1.txt,2.txt,3.txt中打印从1到10的数字

iCaldendar-PHP如何在ics文件上正确应用时区,开始时间和结束时间?

如何从mysql中的开始时间和结束时间获取记录

如何在R中的时间序列中创建指示开始和结束时间段的变量

使用Python / Pandas按小时分组,并带有csv中的开始时间和结束时间日期时间列

如何在JavaScript中获取指定时区的一天的UTC开始时间和结束时间?

如何在熊猫中按开始时间和结束时间组合两个数据帧?

在python中获取当前周的开始和结束时间戳

如何从 FullCalendar 中的事件获取弹出消息中事件的开始和结束时间?

如何在python3中从txt文件中搜索和打印一行?

如何在python中获取数据序列的开始和结束日期时间段?

如何通过Node JS在ffmpeg中剪切特定开始和结束时间的视频?

如何通过Node JS获取ffmpeg中的特定开始和结束时间?

如何调整Outlook中重新出现的会议的开始和结束时间?

如何更改完整日历中的开始和结束时间显示?

如何在PHP中将开始时间和结束时间与当前时间进行比较?

如何限制Kendo DateTimePicker的开始和结束时间?

jQuery如何按小时解析开始和结束时间

如何动态获取多个开始和结束时间的时差