简体   繁体   中英

Compare multiple lines in a text file using python

I'm writing a program that is time tracking report that reports the time that was spent in each virtual machine, I`m having a problem comparing the txt file cause the only thing that changes are the numbers and the hour that each virtual machine was used(that's why I dont Know how to compare them)

09:43:04> --- virtual Desktop 2

12:37:20> --- virtual Desktop 6

23:07:00> --- virtual Desktop 1

23:07:07> --- virtual Desktop 2

23:07:09> --- virtual Desktop 3

12:59:04> --- virtual Desktop 1

13:41:53> --- virtual Desktop 5

13:47:09> --- virtual Desktop 3


def main():


    f = open("/home/lucasfernandes/Desktop/work/DGNET/logthatmatter.txt", "r")

    line = f.readline()
    x = "Desktop"
    while line:
        startime=""
        #print(line)
        line = f.readline()
        #print(line)
        if "--- Taskbarbuttons auf Desktop " in line:
            print(line [41:42])
            #startime = datetime.strptime(line[0:8], '%H:%M:%S')
            #print(startime)
            #print(line)
        else:
            if line[41:42] == "6":
                print(line)


    f.close()

the expected result is

you spent this time on virtual desktop 2: variable

but I don't know how to compare line by line of the txt file

I would read full file and create list with all lines and then get only hour and desktop number from every line.

And then I can use zip(lines, lines[1:]) to have pairs of lines to compare and calculate time between both values

import datetime

# read full file
#text = open(filename).read()

text = '''09:43:04> --- virtual Desktop 2

12:37:20> --- virtual Desktop 6

23:07:00> --- virtual Desktop 1

23:07:07> --- virtual Desktop 2

23:07:09> --- virtual Desktop 3

12:59:04> --- virtual Desktop 1

13:41:53> --- virtual Desktop 5

13:47:09> --- virtual Desktop 3'''

# split on lines, remove empty lines and get only hour and dekstop number
lines = text.split('\n')
lines = [x.strip().split('> --- virtual Desktop ') for x in lines if x.strip()]

#print(lines)


desktop = dict() # for total time for every desktop
diff_24h = datetime.timedelta(days=1) # for some calculations

# works with pairs of lines
for previous, current in zip(lines, lines[1:]):

    prev_hour, prev_desktop = previous
    curr_hour, curr_desktop = current

    print('desktop', prev_desktop, 'start', prev_hour, 'end', curr_hour)

    # covnert to datetime
    prev_dt = datetime.datetime.strptime(prev_hour, '%H:%M:%S')
    curr_dt = datetime.datetime.strptime(curr_hour, '%H:%M:%S')

    # calculate different between hours - if current < prev then there is new day and it need correction diff_24h
    if curr_dt < prev_dt:
        diff_dt = (curr_dt - prev_dt) + diff_24h
    else:
        diff_dt = (curr_dt - prev_dt)

    print('desktop', prev_desktop, 'running', diff_dt)
    print('---')

    # add time to dictionary to get total time
    if prev_desktop not in desktop:
        desktop[prev_desktop] = datetime.timedelta()
    desktop[prev_desktop] += diff_dt

print('--- results ---')    
for key, value in desktop.items():
    print('desktop', key, 'total time', value)

Result:

[['09:43:04', '2'], ['12:37:20', '6'], ['23:07:00', '1'], ['23:07:07', '2'], ['23:07:09', '3'], ['12:59:04', '1'], ['13:41:53', '5'], ['13:47:09', '3']]
desktop 2 start 09:43:04 end 12:37:20
desktop 2 running 2:54:16
---
desktop 6 start 12:37:20 end 23:07:00
desktop 6 running 10:29:40
---
desktop 1 start 23:07:00 end 23:07:07
desktop 1 running 0:00:07
---
desktop 2 start 23:07:07 end 23:07:09
desktop 2 running 0:00:02
---
desktop 3 start 23:07:09 end 12:59:04
desktop 3 running 13:51:55
---
desktop 1 start 12:59:04 end 13:41:53
desktop 1 running 0:42:49
---
desktop 5 start 13:41:53 end 13:47:09
desktop 5 running 0:05:16
---
--- results ---
desktop 2 total time 2:54:18
desktop 6 total time 10:29:40
desktop 1 total time 0:42:56
desktop 3 total time 13:51:55
desktop 5 total time 0:05:16

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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