#---------------------------------------------- # PROBLEM 2 #---------------------------------------------- # # Download the file db.csv from: # http://www.samyzaf.com/braude/PYTHON/projects/db.csv # Write a function db_query(file) for finding all the persons that meet the following criteria: # - They are from Florida or California # - Have blood type B+ or O+ # - Own a Mazda car # - Were born before 1982 # - How many such people did you find? #---------------------------------------------- # SOLUTION: #---------------------------------------------- def create_dict(keys, values): "Create a dictionay from two lists: keys and values of the same length!" d = {} i = 0 for key in keys: d[key] = values[i] i += 1 return d def check_record(d): state = d['State'] blood = d['BloodType'] car = d['Vehicle'] birthday = d['Birthday'] birthyear = int(birthday.split('/')[-1]) if not (state == 'FL' or state == 'CA'): return False if not (blood == 'B+' or state == 'O+'): return False if not 'Mazda' in car: return False if birthyear >= 1982: return False return True def db_query(file): f = open(file, 'r') keys = f.next().strip().split(",") for line in f: values = line.strip().split(',') d = create_dict(keys, values) if check_record(d): print line f.close() # EXAMPLE USAGE #---------------------------------------------- if __name__ == "__main__": file = "D:/BRAUDE/PYTHON/Projects/PROJ2/db.csv" db_query(file)