数据处理
1.原地排序:list.sort
2.复制排序:sorted
3.方法串联:line.strip().split()
4.函数串联:sort(foo(list.item))
5.列表转换:newl=[ s.upper() for s in old_l]
6.工厂函数: uniq_newl=set(newl)
7.分片:print(newl[0:3])
import os
dataPath="F:\book\python\headfirst python book&&code\code\chapter5\data"
def sanitize(time_string):
if ":" in time_string:
min,sec=time_string.split(":")
elif "-" in time_string:
min,sec=time_string.split("-")
if 'min' in locals():
return min+"."+sec
else:
return time_string
try:
fileList=os.listdir(dataPath)
fileData=[]
for fileName in fileList:
print(fileName)
listData=[]
with open(fileName) as dataout:
line=dataout.readline()
"""
for word in line.strip().split(","):
listData.append(sanitize(word))
"""
#转换列表
listData=[sanitize(each_it) for each_it in line.strip().split(",")]
fileData.append(listData)
for data in fileData:
#删除重复数据
"""
tmplist=[]
for item in data:
if item not in tmplist:
tmplist.append(item)
"""
tmplist=set(data) #自动删除重复数据
print(sorted(tmplist)[0:3])
except IOError as err:
dataPath="F:\book\python\headfirst python book&&code\code\chapter5\data"
def sanitize(time_string):
if ":" in time_string:
min,sec=time_string.split(":")
elif "-" in time_string:
min,sec=time_string.split("-")
if 'min' in locals():
return min+"."+sec
else:
return time_string
try:
fileList=os.listdir(dataPath)
fileData=[]
for fileName in fileList:
print(fileName)
listData=[]
with open(fileName) as dataout:
line=dataout.readline()
"""
for word in line.strip().split(","):
listData.append(sanitize(word))
"""
#转换列表
listData=[sanitize(each_it) for each_it in line.strip().split(",")]
fileData.append(listData)
for data in fileData:
#删除重复数据
"""
tmplist=[]
for item in data:
if item not in tmplist:
tmplist.append(item)
"""
tmplist=set(data) #自动删除重复数据
print(sorted(tmplist)[0:3])
except IOError as err:
print("IOError:"+err)