def load(file, season): # connect to database conn = get_connection() # open file for source data rfil = open(file, 'r') # read each line from file _ros = [] for line in rfil: line = line[:-1] fields = line.split(',') d = dict() d['player_id'] = fields[0] d['last_nm'] = fields[1] d['first_nm'] = fields[2] d['bat_hnd'] = fields[3] d['throw_hnd'] = fields[4] d['team_id'] = fields[5] d['fld_pos'] = fields[6] d['season'] = season d['entry_dt'] = datetime.datetime.now() _ros.append(d) # insert into Roster table insert_roster_rec(conn, _ros) # close communication with the database conn.close() rfil.close()
def load(file, season): # open file for source data tfil = open(file, 'r') # connect to database conn = get_connection() cur = conn.cursor() # iterate through the file for line in tfil: line = line[:-1] fields = line.split(',') team_id = fields[0] al_nl = fields[1] tm_city = fields[2] tm_name = fields[3] cur.execute(team_ins_sql, (team_id, al_nl, tm_city, tm_name, season, datetime.datetime.now())) conn.commit() tfil.close() cur.close() conn.close()
def main(): conn = get_connection() ev_recs = retr_game_event_errs(conn) diff = difflib.Differ() for r in ev_recs: game_ndx = r[0] seq_num = r[1] evt_str = r[2] evt_bld = r[3] prfx = '{},{},{},{},'.format(game_ndx, seq_num, evt_str, evt_bld) combo = '' chars = list(diff.compare(evt_str, evt_bld)) for char in chars: combo += char + ',' print(prfx + combo) conn.close()
def main(): print('Event Parser: begin.') # by game index #parse_event(getconnection(), 20180) # by home team #parse_event(getconnection(), 'ANA') # by number range conn = get_connection() games = retr_game_index_list(conn) for game in games: game_ndx = game[0] print('parsing game index -> {}'.format(game_ndx)) parse_event(conn, game_ndx) print('Event Parser: end.') conn.close()
def main(): print('Begin...') fname = '/Users/greggmidon/data/bbx/parks.csv' print('Reading file: ' + fname) # open file for source data src = open(fname, 'r') # connect to database conn = get_connection() cur = conn.cursor() # iterate through the file for line in src: line = line[:-1] fields = line.split(',') park_id = fields[0] park_name = fields[1] alt_names = fields[2] city = fields[3] state = fields[4] open_date = fields[5] close_date = fields[6] if close_date == '': close_date = None league = fields[7] cur.execute(park_ins,(park_id,park_name,alt_names,city,state,open_date,close_date,league)) conn.commit() src.close() cur.close() conn.close()
def load(file, season): # connect to database conn = get_connection() # source file tfil = open(file, 'r') # iterate through the file firsttime = True lnum = 0 for line in tfil: line = line[:-1] recordList = line.split(',') rectyp = recordList[0] if rectyp == 'id': id = recordList[1] #print(' -> ' + id) index = retr_game_index(conn) if firsttime: firsttime = False else: insert_info_rec(conn, _info) insert_start_rec(conn, _start) insert_sub_rec(conn, _sub) insert_data_rec(conn, _data) insert_play_rec(conn, _play) # setup for next game _info = dict() _info['id'] = id _info['index'] = index _info['season'] = season _start = [] _play = [] _sub = [] _data = [] lnum = 0 elif rectyp == 'info': rkey = recordList[1] rval = recordList[2] _info[rkey] = rval elif rectyp == 'start': d = dict() d['game_ndx'] = index d['player_id'] = recordList[1] d['player_nm'] = recordList[2] d['visit_home'] = recordList[3] d['order'] = recordList[4] d['position'] = recordList[5] d['entry_dt'] = datetime.datetime.now() _start.append(d) elif rectyp == 'play': lnum += 1 d = dict() d['game_ndx'] = index d['seq_num'] = lnum d['inning'] = recordList[1] d['visit_home'] = recordList[2] d['player_id'] = recordList[3] d['pitch_cnt'] = recordList[4] d['pitches'] = recordList[5] d['event_str'] = recordList[6] d['entry_dt'] = datetime.datetime.now() _play.append(d) elif rectyp == 'sub': lnum += 1 d = dict() d['game_ndx'] = index d['seq_num'] = lnum d['player_id'] = recordList[1] d['player_nm'] = recordList[2] d['visit_home'] = recordList[3] d['order'] = recordList[4] d['position'] = recordList[5] d['entry_dt'] = datetime.datetime.now() _sub.append(d) elif rectyp == 'data': d = dict() d['game_ndx'] = index d['data_typ'] = recordList[1] d['player_id'] = recordList[2] d['smint_val'] = recordList[3] d['entry_dt'] = datetime.datetime.now() _data.append(d) insert_info_rec(conn, _info) insert_start_rec(conn, _start) insert_sub_rec(conn, _sub) insert_data_rec(conn, _data) insert_play_rec(conn, _play) # close communication with the database conn.close() tfil.close()