数据建模
说人话:就是解决如何把原始的文本形式的数据文件,转换成按照预先定制类的一个个实例的方法,从而方便用户调用、处理。
上图来自《head First Python》P247
上图来自《head First Python》P249
实现——数据存储和访问
数据存储和访问是有两个函数实现的:
put_to_store()函数
get_from_store()函数。
- put_to_store()函数
返回值:
- AthleteList字典,包含Athlete对象;
- 将AthleteList写入pickle中;
代码:
def put_to_store(files_list):
all_athletes = {}
#定义字典,用于存储运动员对象
for each_file in files_list:
#get_coach_data(each_file)打开文件列表中的每一个文件,
#并将其转换成AthleteList对象。
#最后,将AthleteList对象存入all_athletes字典。
ath = get_coach_data(each_file)
all_athletes[ath.name] = ath
try:
#打开athletes.pickle文件,将字典 all_athletes存入其中。
with open('athletes.pickle','wb') as athf:
pickle.dump(all_athletes, athf)
except IOError as ioerr:
print('File error(put_to_store):' + str(ioerr))
return(all_athletes)
- get_from_store()函数
返回值:
- AthleteList字典,包含Athlete对象;
代码:
def get_from_store():
all_athletes = {}
try:
#打开athletes.pickle文件,它是一个字典
#然后,将其赋值给字典all_athletes。
with open('athletes.pickle','rb') as athf:
all_athletes = pickle.load(athf)
except IOError as ioerr:
print('File error(get_from_store):' + str(ioerr))
return(all_athletes)
验证——数据存储和访问
负责存储和访问数据的代码模块(athletemodel.py)如下:
# -*- coding:utf-8 -
'''
Created on 2017-1-3
@author: lenovo
'''
import pickle
from athletelist import AthleteList
def get_coach_data(filename):
try:
with open(filename) as jaf:
data=jaf.readline()
templ = data.strip().split(',')
return(AthleteList(templ.pop(0),templ.pop(0),templ))
except IOError as ioerr:
print('File error:'+str(ioerr))
return(None)
def put_to_store(files_list):
all_athletes = {}
#定义字典,用于存储运动员对象
for each_file in files_list:
ath = get_coach_data(each_file)
all_athletes[ath.name] = ath
try:
with open('athletes.pickle','wb') as athf:
pickle.dump(all_athletes, athf)
except IOError as ioerr:
print('File error(put_to_store):' + str(ioerr))
return(all_athletes)
def get_from_store():
all_athletes = {}
try:
with open('athletes.picke','rb') as athf:
all_athletes = pickle.load(athf)
except IOError as ioerr:
print('File error(get_from_store):' + str(ioerr))
return(all_athletes)
Ok,来验证一下!
在IDLE中,打开athletemodel.py,按F5运行,然后,输入命令dir()
,将会看到:
>>> dir()
['AthleteList', '__builtins__', '__doc__', '__file__', '__name__', '__package__', 'get_coach_data', 'get_from_store', 'pickle', 'put_to_store']
那
dir()
的作用是什么呢?“Python中内置的dir()函数用于列出一个定义对象的标识符。例如,对于一个模块,包括在模块中定义的函数,类和变量。当你给dir()提供一个模块名字时,它返回在那个模块中定义的名字的列表。当没有为其提供参数时, 它返回当前模块中定义的变量、属性、方法的列表。”
因此,我们可以发现,通过输入
dir()
它返回了当前模块中定义的方法、属性和变量的列表。
Ok,工具有了,我们设定要读取并存储的文件:
>>> the_files = ['sarah2.txt','james2.txt','mikey2.txt','julie2.txt']
存储数据:
>>> data = put_to_store(the_files)
查看一下:
>>> data
{'James Lee': ['2-34', '3:21', '2.34', '2.45', '3.01', '2:01', '2:01', '3:10', '2-22', '2-01', '2.01', '2:16'], 'Sarah Sweeney': ['2:58', '2.58', '2:39', '2-25', '2-55', '2:54', '2.18', '2:55', '2:55', '2:22', '2-21', '2.22'], 'Julie Jones': ['2.59', '2.11', '2:11', '2:23', '3-10', '2-23', '3:10', '3.21', '3-21', '3.01', '3.02', '2:59'], 'Mikey McManus': ['2:22', '3.01', '3:01', '3.02', '3:02', '3.02', '3:22', '2.49', '2:38', '2:40', '2.22', '2-31']}
果然,调用函数put_to_store(the_files)
后,将文本文件转换成了字典。
数据存储好了(转换成实例了),下面来访问吧:
调用
get_from_store()
函数
>>> data_copy = get_from_store()
函数返回字典给data_copy,然后可以访问:
>>> data_copy = get_from_store()
>>> for each_athlete in data_copy:
print(data_copy[each_athlete].name + ' ' + data_copy[each_athlete].dob)
#注意调用的格式,字典中的每一个数据项都是AthleteList的对象
James Lee 2002-3-14
Sarah Sweeney 2002-6-17
Julie Jones 2002-8-17
Mikey McManus 2002-2-24
Ok,至此,有关数据的存储和读取方法告一段落。另,第一次用Markdown写文档,确实很简洁、很好用;对比word中需要反复调整格式,真的是太方便了,感谢开发者!!