Python read from text file
You are given a file called logIn.txt that lists log-on and log-off times for users of a system. A typical line of the file looks like this:
Momir, 14:22, 14:37
Each line has three entries separated by commas: a username, a log-on time, and a log-off time. Times are given in 24-hour format. You may assume that all log-ons and log-offs occur within a single workday.
Write a program that scans through the file and prints out all users who were online for at
least an hour.
logIn = [line.strip() for line in open('C:/Users/ZM/Desktop/Documents/New
folder/logIn.txt')]
logIn1 = [line.split(', ') for line in logIn]
print(logIn1)
L = []
for i in range(len(logIn1)):
for j in range(len(logIn1[i])):
hour1 = logIn1[i][1][:2]
minutes1 = logIn1[i][1][3:5]
hour2 = logIn1[i][2][:2]
minutes2 = logIn1[i][2][3:5]
if int(hour1) > int(hour2):
time = 24%int(hour1) + int(hour2)
if time <= 1:
break
else:
if logIn1[i][0] not in L:
L.append(logIn1[i][0])
elif int(hour2) - int(hour1) == 1 and int(minutes1) > int(minutes2) :
break
elif int(hour2) == int(hour1) and int(minutes1) + int(minutes2) > 60:
break
else:
if logIn1[i][0] not in L:
L.append(logIn1[i][0])
print(hour1, minutes1, hour2, minutes2)
hour1 = 0
minutes1 = 0
hour2 = 0
minutes2 = 0
print(L)
logIn1 = [line.split(', ') for line in logIn]
print(logIn1)
L = []
for i in range(len(logIn1)):
for j in range(len(logIn1[i])):
hour1 = logIn1[i][1][:2]
minutes1 = logIn1[i][1][3:5]
hour2 = logIn1[i][2][:2]
minutes2 = logIn1[i][2][3:5]
if int(hour1) > int(hour2):
time = 24%int(hour1) + int(hour2)
if time <= 1:
break
else:
if logIn1[i][0] not in L:
L.append(logIn1[i][0])
elif int(hour2) - int(hour1) == 1 and int(minutes1) > int(minutes2) :
break
elif int(hour2) == int(hour1) and int(minutes1) + int(minutes2) > 60:
break
else:
if logIn1[i][0] not in L:
L.append(logIn1[i][0])
print(hour1, minutes1, hour2, minutes2)
hour1 = 0
minutes1 = 0
hour2 = 0
minutes2 = 0
print(L)
[['momir', '14:28', '15:28'], ['mateja', '18:26', '20:36'], ['nikolina', '00:15', '02:21'], ['sasa', '23:45', '00:26'], ['radisa', '10:28', '10:55'], ['nikola', '18:01', '19:00'], ['ratko', '21:28', '02:45']]
14 28 15 28
18 26 20 36
00 15 02 21
23 45 00 26
10 28 10 55
18 01 19 00
21 28 02 45
['momir', 'mateja', 'nikolina', 'ratko']
Коментари
Постави коментар