读取4个文件内容,格式化数据,升序,显示每个文件前3个数据
julie.txt
2.59,2.11,2:11,2:23,3-10,2-23,3:10,3.21,3-21
james.txt
2-34,3:21,2.34,2.45,3.01,2:01,2:01,3:10,2-22
sarah.txt
2:58,2.58,2:39,2-25,2-55,2:54,2.18,2:55,2:55
mikey.txt
2:22,3.01,3:01,3.02,3:02,3.02,3:22,2.49,2:38
chapter5.py
def get_coach_data(filename):
try:
with open(filename) as f:
data = f.readline()
return ( data.strip().split(','))
except IOError as err:
print('File err:' + str(err))
return(None) def sanitize(time_string):
if '-' in time_string:
splitter = '-'
elif ':' in time_string:
splitter = ':'
else:
return(time_string) (mins, secs) = time_string.split(splitter)
return(mins + '.' + secs) try:
julie = get_coach_data('julie.txt')
james = get_coach_data('james.txt')
sarah = get_coach_data('sarah.txt')
mikey = get_coach_data('mikey.txt') print( sorted( set ([sanitize(s) for s in julie]) )[0:3] )
print( sorted( set ([sanitize(s) for s in james]) )[0:3] )
print( sorted( set ([sanitize(s) for s in sarah]) )[0:3] )
print( sorted( set ([sanitize(s) for s in mikey]) )[0:3] ) except IOError as err:
print("file error" + str(err))