import sanitize
import get_data
#(1)方法一:对文件一个个进行打开以及去空格等处理成一个字符串的列表
'''with open('james.txt') as jaf:
data = jaf.readline()
james=data.strip().split(',')
with open('julie.txt') as jul:
data = jul.readline()
julie=data.strip().split(',')
with open('mikey.txt') as mik:
data = mik.readline()
mikey=data.strip().split(',')
with open('sarah.txt') as sar:
data = sar.readline()
sarah=data.strip().split(',')'''
#方法二:利用函数将上面的步骤简化
james=get_data.get_data('james.txt')
print(james)
#(2)方法一:创建列表,迭代,转换,追加
'''clean_james=[]
clean_julie=[]
clean_mikey=[]
clean_sarah=[]
for each_t in james:
clean_james.append(sanitize.sanitize(each_t))
for each_t in julie:
clean_julie.append(sanitize.sanitize(each_t))
for each_t in mikey:
clean_mikey.append(sanitize.sanitize(each_t))
for each_t in sarah:
clean_sarah.append(sanitize.sanitize(each_t))
print(sorted(clean_james))
print(sorted(clean_julie))
print(sorted(clean_mikey))
print(sorted(clean_sarah))'''
#方法二:使用列表推导
'''print(sorted([sanitize.sanitize(each_t) for each_t in james]))
print(sorted([sanitize.sanitize(each_t) for each_t in julie]))
print(sorted([sanitize.sanitize(each_t) for each_t in mikey]))
print(sorted([sanitize.sanitize(each_t) for each_t in sarah]))'''
#(3)方法一:按要求输出成绩最好的前三个成绩,且不能重复,创建列表,迭代,追加
'''james2=sorted([sanitize.sanitize(each_t) for each_t in james])
use_james=[]
for each_t in james2:
if each_t not in use_james:
use_james.append(each_t)
print(use_james[0:3])'''
#方法二:按要求输出成绩最好的前三个成绩,且不能重复,set()集合方法,专门用来去除重复项
#print(sorted(set([sanitize.sanitize(each_t) for each_t in james]))[0:3])
def sanitize(time_string):
if '-' in time_string:
splitter='-'
elif ':' in time_string:
splitter=':'
else:
return(time_string)
(min,sec)=time_string.split(splitter)
return (min+'.'+sec)
def get_data(filename):
try:
with open(filename) as f:
data = f.readline()
return(data.strip().split(','))
except IOError as err:
print("File error:"+str(err))
return(None)
NOTE:1)调用的文件**.py都必须在一个文件夹中
2)不同.py中利用import调用,代码中一定要加上模块名即*.py的名字,再加上函数名称
3)注意这里
print(sorted(set([sanitize.sanitize(each_t) for each_t in james]))[0:3])
[0:3]是指从列表中第一个开始的前三个数据项,并非从索引号0到3的四个数据项
4)sorted()处理的是列表中的每个数据项,并不是整个列表