def getCoachData(fileName): with open(fileName) as file: personData = file.readline().strip().split(',') return ({ 'name': personData.pop(0), 'dob': personData.pop(0), 'times': str( sorted( set([ sanitizer.sanitizeString(line) for line in personData ]))[0:3]) })
def getTimes(name): return (str( sorted(set([sanitizer.sanitizeString(line) for line in name]))[0:3]))
def top3(self): return(sorted(set([sanitizer.sanitizeString(line) for line in self]))[0:3])
# Head first Python # Page 157 import sanitizer import os os.chdir('~/Desktop/data') with open('james.txt') as jamesFile: james = jamesFile.readline().strip().split(',') with open('julie.txt') as julieFile: julie = julieFile.readline().strip().split(',') with open('mikey.txt') as mikeyFile: mikey = mikeyFile.readline().strip().split(',') with open('sarah.txt') as sarahFile: sarah = sarahFile.readline().strip().split(',') print(sorted([sanitizer.sanitizeString(line) for line in james])) print(sorted([sanitizer.sanitizeString(line) for line in julie])) print(sorted([sanitizer.sanitizeString(line) for line in mikey])) print(sorted([sanitizer.sanitizeString(line) for line in sarah]))
# Page 161 import sanitizer import os os.chdir('~/Desktop/data') with open('james.txt') as jamesFile: james = jamesFile.readline().strip().split(',') with open('julie.txt') as julieFile: julie = julieFile.readline().strip().split(',') with open('mikey.txt') as mikeyFile: mikey = mikeyFile.readline().strip().split(',') with open('sarah.txt') as sarahFile: sarah = sarahFile.readline().strip().split(',') james = sorted([sanitizer.sanitizeString(line) for line in james]) uniqueJames = [] for i in james: if i not in uniqueJames: uniqueJames.append(i) print(uniqueJames[0:3]) julie = sorted([sanitizer.sanitizeString(line) for line in julie]) uniqueJulie = [] for i in julie: if i not in uniqueJulie: uniqueJulie.append(i) print(uniqueJulie[0:3]) mikey = sorted([sanitizer.sanitizeString(line) for line in mikey]) uniqueMikey = []
def printScores(name): print( sorted(set([sanitizer.sanitizeString(line) for line in name]))[0:3])
# return(mins + '.' + secs) # The main code with open('james.txt') as jamesFile: james = jamesFile.readline().strip().split(',') with open('julie.txt') as julieFile: julie = julieFile.readline().strip().split(',') with open('mikey.txt') as mikeyFile: mikey = mikeyFile.readline().strip().split(',') with open('sarah.txt') as sarahFile: sarah=sarahFile.readline().strip().split(',') newJames = [] newJulie = [] newMikey = [] newSarah = [] for line in james: newJames.append(sanitizer.sanitizeString(line)) for line in julie: newJulie.append(sanitizer.sanitizeString(line)) for line in mikey: newMikey.append(sanitizer.sanitizeString(line)) for line in sarah: newSarah.append(sanitizer.sanitizeString(line)) print(sorted(newJames)) print(sorted(newJulie)) print(sorted(newMikey)) print(sorted(newSarah))
with open(fileName) as file: fileName = file.readline().strip().split(',') return(fileName) def getTimes(name): return(str(sorted(set([sanitizer.sanitizeString(line) for line in name]))[0:3])) # def gietNameTimes(newFileName): # name = getCoachData(newFileName) # (personName, personTime) = name.pop(0), name.pop(0) # return(personName + 's top times are '+ getTimes(name)) # print(getNameTimes('james2.txt')) # print(getNameTimes('julie2.txt')) # print(getNameTimes('mikey2.txt')) # print(getNameTimes('sarah2.txt')) sarah = getCoachData('sarah2.txt') sarahData = {} sarahData['name'] = sarah.pop(0) sarahData['dob'] = sarah.pop(0) sarahData['times'] = sarah sarahData['name', 'dob', 'times'] = sarah.pop(0), sarah.pop(0), sarah print(sarahData['name'] + 's top times are '+ str(sorted(set([sanitizer.sanitizeString(line) for line in sarahData['times']]))[0:3])) except IOError as err: print('IO Error!' + str(err))
# Head first Python # Page 156 import sanitizer mins = [1, 2, 3] secs = [i * 60 for i in mins] print(secs) meters = [1, 10, 2] feet = [i * 3.281 for i in meters] print(feet) lower = ["I", "don't", "like", "spam"] upper = [i.upper() for i in lower] print(lower) dirty = ['2-22', '2:22', '2.22'] clean = [sanitizer.sanitizeString(i) for i in dirty] print(clean) clean = [float(i) for i in clean] print(clean) clean = [float(sanitizer.sanitizeString(i)) for i in dirty] print(clean)