從零學(xué)python系列之?dāng)?shù)據(jù)處理編程實(shí)例(二)
在上一節(jié)從零學(xué)python系列之?dāng)?shù)據(jù)處理編程實(shí)例(一)的基礎(chǔ)上數(shù)據(jù)發(fā)生了變化,文件中除了學(xué)生的成績外,新增了學(xué)生姓名和出生年月的信息,因此將要成變成:分別根據(jù)姓名輸出每個(gè)學(xué)生的無重復(fù)的前三個(gè)最好成績和出生年月
數(shù)據(jù)準(zhǔn)備:分別建立四個(gè)文本文件
james2.txt James Lee,2002-3-14,2-34,3:21,2.34,2.45,3.01,2:01,2:01,3:10,2-22
julie2.txt Julie Jones,2002-8-17,2.59,2.11,2:11,2:23,3-10,2-23,3:10,3.21,3-21
mikey2.txt Mikey McManus,2002-2-24,2:22,3.01,3:01,3.02,3:02,3.02,3:22,2.49,2:38
sarah2.txt Sarah Sweeney,2002-6-17,2:58,2.58,2:39,2-25,2-55,2:54,2.18,2:55,2:55
在上一節(jié)基礎(chǔ)上,修改部分代碼,將新要求實(shí)現(xiàn)如下:
import os
print(os.getcwd())
os.chdir('C:\Python33\HeadFirstPython\hfpy_code\chapter6') #將工作空間修改為文件所在的目錄
#定義函數(shù)get_filedata從文件中取值
def get_filedata(filename):
try:
with open(filename) as f: #with語句打開和自動(dòng)關(guān)閉文件
data=f.readline() #從文件中逐行讀取字符
data_list=data.strip().split(',') #將字符間的空格清除后,用逗號(hào)分隔字符
return({
"name" : data_list.pop(0),
"date_of_birth" : data_list.pop(0),
"times" : str(sorted(set([modify_time_format(s) for s in data_list]))[0:3])
}) #使用字典將關(guān)聯(lián)的姓名,出生年月,時(shí)間鍵和值進(jìn)行存儲(chǔ)并返回
except IOError as ioerr:
print ('File Error' + str(ioerr)) #異常處理,打印錯(cuò)誤
return (None)
#定義函數(shù)modify_time_format將所有文件中的時(shí)分表達(dá)方式統(tǒng)一為“分.秒”
def modify_time_format(time_string):
if "-" in time_string:
splitter="-"
elif ":" in time_string:
splitter=":"
else:
splitter="."
(mins, secs)=time_string.split(splitter) #用分隔符splitter分隔字符后分別存入mins和secs
return (mins+ '.' +secs)
#定義函數(shù)get_prev_three返回文件中排名前三的不重復(fù)的時(shí)間成績
def get_prev_three(filename):
new_list=[modify_time_format(each_t) for each_t in get_filedata(filename)] #采用列表推導(dǎo)將統(tǒng)一時(shí)分表達(dá)方式后的記錄生成新的列表
delete_repetition=set(new_list) #采用集合set函數(shù)刪除新列表中重復(fù)項(xiàng),并生成新的集合
in_order=sorted(delete_repetition) #采用復(fù)制排序sorted函數(shù)對(duì)無重復(fù)性的新集合進(jìn)行排序
return (in_order[0:3])
#輸出james的排名前三的不重復(fù)成績和出生年月
james = get_filedata('james2.txt')
print (james["name"]+"'s fastest times are: " + james["times"])
print (james["name"] + "'s birthday is: " + james["date_of_birth"])
#輸出julie的排名前三的不重復(fù)成績和出生年月
julie = get_filedata('julie2.txt')
print (julie["name"]+"'s fastest times are: " + julie["times"])
print (julie["name"] + "'s birthday is: " + julie["date_of_birth"])
#輸出mikey的排名前三的不重復(fù)成績和出生年月
mikey = get_filedata('mikey2.txt')
print (mikey["name"]+"'s fastest times are: " + mikey["times"])
print (mikey["name"] + "'s birthday is: " + mikey["date_of_birth"])
#輸出sarah的排名前三的不重復(fù)成績和出生年月
sarah = get_filedata('sarah2.txt')
print (sarah["name"]+"'s fastest times are: " + sarah["times"])
print (sarah["name"] + "'s birthday is: " + sarah["date_of_birth"])
通過建立繼承內(nèi)置list的類AthleteList,將方法定義在類中實(shí)現(xiàn)相同功能:
import os
print(os.getcwd())
os.chdir('C:\Python33\HeadFirstPython\hfpy_code\chapter6') #將工作空間修改為文件所在的目錄
#定義類AthleteList繼承python內(nèi)置的list
class AthleteList(list):
def __init__(self, name, dob=None, times=[]):
list.__init__([])
self.name=name
self.dob=dob
self.extend(times)
def get_prev_three(self):
return (sorted(set([modify_time_format(t) for t in self]))[0:3])
def get_filedata(filename):
try:
with open(filename) as f: #with語句打開和自動(dòng)關(guān)閉文件
data=f.readline() #從文件中逐行讀取字符
data_list=data.strip().split(',') #將字符間的空格清除后,用逗號(hào)分隔字符
return(
AthleteList(data_list.pop(0), data_list.pop(0), data_list)
) #使用字典將關(guān)聯(lián)的姓名,出生年月,時(shí)間鍵和值進(jìn)行存儲(chǔ)并返回
except IOError as ioerr:
print ('File Error' + str(ioerr)) #異常處理,打印錯(cuò)誤
return (None)
def modify_time_format(time_string):
if "-" in time_string:
splitter="-"
elif ":" in time_string:
splitter=":"
else:
splitter="."
(mins, secs)=time_string.split(splitter) #用分隔符splitter分隔字符后分別存入mins和secs
return (mins+ '.' +secs)
james = get_filedata('james2.txt')
print (james.name+"'s fastest times are: " + str(james.get_prev_three()))
julie = get_filedata('julie2.txt')
print (julie.name+"'s fastest times are: " + str(julie.get_prev_three()))
mikey = get_filedata('mikey2.txt')
print (mikey.name+"'s fastest times are: " + str(mikey.get_prev_three()))
sarah = get_filedata('sarah2.txt')
print (sarah.name+"'s fastest times are: " + str(sarah.get_prev_three()))
- 對(duì)python .txt文件讀取及數(shù)據(jù)處理方法總結(jié)
- python數(shù)據(jù)處理實(shí)戰(zhàn)(必看篇)
- 從零學(xué)python系列之?dāng)?shù)據(jù)處理編程實(shí)例(一)
- 基于python爬蟲數(shù)據(jù)處理(詳解)
- Python數(shù)據(jù)處理numpy.median的實(shí)例講解
- Python 數(shù)據(jù)處理庫 pandas 入門教程基本操作
- 對(duì)python 數(shù)據(jù)處理中的LabelEncoder 和 OneHotEncoder詳解
- python實(shí)現(xiàn)爬蟲統(tǒng)計(jì)學(xué)校BBS男女比例之?dāng)?shù)據(jù)處理(三)
- python 解決動(dòng)態(tài)的定義變量名,并給其賦值的方法(大數(shù)據(jù)處理)
- python文本數(shù)據(jù)處理學(xué)習(xí)筆記詳解
相關(guān)文章
Tensorflow中使用tfrecord方式讀取數(shù)據(jù)的方法
這篇文章主要介紹了Tensorflow中使用tfrecord方式讀取數(shù)據(jù)的方法,適用于數(shù)據(jù)較多時(shí),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06
tensorflow入門:tfrecord 和tf.data.TFRecordDataset的使用
今天小編就為大家分享一篇tensorflow入門:tfrecord 和tf.data.TFRecordDataset的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-01-01
詳解python命令提示符窗口下如何運(yùn)行python腳本
這篇文章主要介紹了詳解python命令提示符窗口下如何運(yùn)行python腳本,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Python實(shí)現(xiàn)一個(gè)轉(zhuǎn)存純真IP數(shù)據(jù)庫的腳本分享
工作中我們常需要使用純真IP數(shù)據(jù)庫內(nèi)的數(shù)據(jù)做分析,下面這篇文章主要給大家介紹了利用Python如何實(shí)現(xiàn)一個(gè)轉(zhuǎn)存純真IP數(shù)據(jù)庫的相關(guān)資料,對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-05-05
利用Python實(shí)現(xiàn)簡單的Excel統(tǒng)計(jì)函數(shù)
這篇文章主要介紹了利用Python實(shí)現(xiàn)簡單的Excel統(tǒng)計(jì)函數(shù),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-07-07

