您好,我有此列表,我想在几分钟内将其转换为相同的列表:
TR898 09:05
JX965 09:10
RE741 09:12
FR898 09:15
TY471 09:18
GE896 09:19
GE897 09:19
GE898 09:19
TZ423 09:20
TR453 09:20
TY473 09:21
TN423 09:21
XY423 09:23
GT423 09:25
TY513 09:21
。
Proposed_file_name = 'proposed.txt'
infile = open(Proposed_file_name, 'r')
i=0
line = infile.readlines()
while line != '':
i=i+1
part=line[1+i].split()
IDS=part[0]
dpart=(part[1].split(':'))
hour=int(dpart[0])
mints=int(dpart[1])
total=(hour*60+mints)
line = infile.readline()
else:
infile.close()
print (total)
答案 0 :(得分:0)
编辑了整个答案。运行以下代码:
with open("proposed.txt", "r") as inFile:
lines = [line for line in inFile]
with open("out.txt", "w") as outFile:
minuteTimes = []
IDs = []
newTimes = []
for line in lines:
content = line.split()
IDs.append(content[0])
time = content[1].split(":")
finalTime = int(time[0]) * 60 + int(time[1])
minuteTimes.append(finalTime)
#i = 0
newTime = minuteTimes[0] + 5
newTimes.append((IDs[0], minuteTimes[0], 0))
canGo = True
for i in range(1,len(minuteTimes)):
diff = minuteTimes[i] - minuteTimes[i-1]
newDiff = newTime - minuteTimes[i]
if diff == 5:
newTimes.append((IDs[i], minuteTimes[i], diff))
newTime = minuteTimes[i] + 5
elif diff < 5:
if newDiff > 15:
i += 1
else:
minuteTimes[i] = newTime
newTimes.append((IDs[i], minuteTimes[i], diff))
newTime = minuteTimes[i] + 5
else:
newTimes.append((IDs[i], minuteTimes[i], diff))
newTime = minuteTimes[i] + 5
for n in newTimes:
currentID = n[0]
minutes = str(n[1] // 60).zfill(2)
seconds = str(n[1] % 60).zfill(2)
outFile.write(currentID + " " + minutes + ":" + seconds + '\n')
将为您提供所需的输出:
TR898 09:05
JX965 09:10
RE741 09:15
FR898 09:20
TY471 09:25
GE896 09:30
TZ423 09:35
GT423 09:40
ZZSAQ 11:52
答案 1 :(得分:0)
这就是我尝试过的
with open("proposed.txt", "r") as inFile:
lines = [line for line in inFile]
with open("out.txt", "w") as outFile:
for line in lines:
content = line.split()
time = content[1].split(":")
finalTime = int(time[0]) * 60 + int(time[1])
finalString = " ".join([content[0], str(finalTime), '\n'])
outFile.write(finalString)
while (finalTime-finalTime<5):
Final=(finalTime-finalTime==5)
outFile.write(Final)
if Final-finaltime>15:
OutFile.write(Line)
这是所需的输出:
TR898 09:05
JX965 09:10
RE741 09:15
FR898 09:20
TY471 09:25
GE896 09:30
TZ423 09:35
GT423 09:40