Skip to main content
replaced http://dba.stackexchange.com/ with https://dba.stackexchange.com/
Source Link

I ended up using a python script to output to a txt file that I could upload to a MySQL table. But In the interest of this exchange, mendosi's answermendosi's answer is definitely the way to go.

I ended up using a python script to output to a txt file that I could upload to a MySQL table. But In the interest of this exchange, mendosi's answer is definitely the way to go.

I ended up using a python script to output to a txt file that I could upload to a MySQL table. But In the interest of this exchange, mendosi's answer is definitely the way to go.

Source Link
Paul White
  • 90.3k
  • 30
  • 424
  • 663

Community Wiki answer created to hold the solution the question author found

I ended up using a python script to output to a txt file that I could upload to a MySQL table. But In the interest of this exchange, mendosi's answer is definitely the way to go.

The python for any with similar issues:

import os
import numpy as np

global players, games
players = {}
visit_players = [101] + list(range(105, 132, 3))
home_players  = [103] + list(range(132, 159, 3))
visit_stats   = list(range(21, 49))
home_stats    = list(range(49, 77))
scores        = list(range(9, 11))

path  = "path to data txt files"
games = np.zeros((177789, 28 * 20 + 2))
files = os.listdir(path)
num   = 0

def add_player(player, stat) :
    for p in player:
        if p in players:
            times = players[p][-1]
            players[p][:-1] = (players[p][:-1] + stat) * times / (times + 1)
            players[p][-1] += 1
        else:
            players[p] = np.array([x for x in stat] + [1])

def add_games(player, num, offset):
    length = range(len(player))
    for i in length:
        p = player[i]
        i += offset
        if p in players:
            games[num, i*28:(i+1)*28] = players[p][:-1]
        else:
            games[num, i*28:(i+1)*28] = np.zeros((1, 28))

            
            
for file_name in files:
    with open(path + file_name, 'rt', encoding='utf-8', errors='ignore') as f:
        for line in f:
            contents       = line.split(",")
            score          = [int(contents[x]) for x in scores]
            games[num, -2] = score[0]
            games[num, -1] = score[1]
            try:
                visit_stat   = np.array([int(contents[x]) for x in visit_stats])
                home_stat    = np.array([int(contents[x]) for x in home_stats])
            except:
                continue
            visit_player = np.array([contents[x] for x in visit_players])
            home_player  = np.array([contents[x] for x in home_players])
            add_games(visit_player, num, 0)
            add_games(home_player, num, 10)
            add_player(visit_player, visit_stat)
            add_player(home_player, home_stat)

            num += 1
Post Made Community Wiki by Paul White