使用pickle保存,读取文件
#dump.py
import pickle
try:
with open(‘data.pickle‘,‘wb‘) as data:
pickle.dump([1,2,‘three‘],data)
except IOError as err:
print(‘file error:‘ + str(err))
except pickle.PickleError as perr:
print(‘pickling error:‘ + str(perr))
#load.py
import pickle
try:
with open(‘data.pickle‘,‘rb‘) as data:
a_list = pickle.load(data)
print a_list
except IOError as err:
print(‘file error:‘+str(err))
except pickle.PickleError as perr:
print(‘pickling error:‘+str(perr))
$ python dump.py
$ python load.py
[1, 2, ‘three‘]
第五章 处理数据
读取从head first python网站下载的四个成绩数据并格式化数据:
#print_times.py
def print_times(file_name):
time_list = []
try:
with open(file_name+‘.txt‘) as data:
time_list = data.read().replace(‘-‘,‘:‘).replace(‘.‘,‘:‘)
time_list = time_list.strip().split(‘,‘)
except IOError as err:
print(‘file error:‘+str(err))
return time_list
打印测试:
>>> import print_times
>>> james = print_times.print_times(‘james‘)
>>> james
[‘2-34‘, ‘3:21‘, ‘2.34‘, ‘2.45‘, ‘3.01‘, ‘2:01‘, ‘2:01‘, ‘3:10‘, ‘2-22‘]
>>> julie = print_times.print_times(‘julie‘)
>>> julie
[‘2.59‘, ‘2.11‘, ‘2:11‘, ‘2:23‘, ‘3-10‘, ‘2-23‘, ‘3:10‘, ‘3.21‘, ‘3-21‘]
>>> mikey = print_times.print_times(‘mikey‘)
>>> mikey
[‘2:22‘, ‘3.01‘, ‘3:01‘, ‘3.02‘, ‘3:02‘, ‘3.02‘, ‘3:22‘, ‘2.49‘, ‘2:38‘]
>>> sarah = print_times.print_times(‘sarah‘)
>>> sarah
[‘2:58‘, ‘2.58‘, ‘2:39‘, ‘2-25‘, ‘2-55‘, ‘2:54‘, ‘2.18‘, ‘2:55‘, ‘2:55‘]
>>>
- mac终端中可用ctrl+a,ctrl+e快捷键跳至行首或行尾
推导列表
将一个列表以某种规则转换为另一个列表
>>>james = print_times.print_times(‘james‘)
>>> james
[‘2:34‘, ‘3:21‘, ‘2:34‘, ‘2:45‘, ‘3:01‘, ‘2:01‘, ‘2:01‘, ‘3:10‘, ‘2:22‘]
>>> jamesLee = [t.replace(‘:‘,‘-‘) for t in james]
>>> jamesLee
[‘2-34‘, ‘3-21‘, ‘2-34‘, ‘2-45‘, ‘3-01‘, ‘2-01‘, ‘2-01‘, ‘3-10‘, ‘2-22‘]
排序
- sort()提供原地排序,不保存原来的顺序
- sorted()复制排序,返回一个排序后的副本
- 默认排序方式为升序,降序可传参数reverse=True
使用集合去除列表重复项
>>> james
[‘2:01‘, ‘2:01‘, ‘2:22‘, ‘2:34‘, ‘2:34‘, ‘2:45‘, ‘3:01‘, ‘3:10‘, ‘3:21‘]
>>> unique = set(james)
>>> unique
set([‘2:45‘, ‘3:21‘, ‘2:01‘, ‘2:22‘, ‘2:34‘, ‘3:10‘, ‘3:01‘])
>>> james = list(unique)
>>> james
[‘2:45‘, ‘3:21‘, ‘2:01‘, ‘2:22‘, ‘2:34‘, ‘3:10‘, ‘3:01‘]
时间: 2024-10-14 03:38:30