fantastyczne_gole/notebooks/dataExtraction.ipynb
2023-12-04 11:06:06 +01:00

3.7 KiB

# librarys:

import json
import csv
import os
import pandas as pd
# Functions

def openGameFile(name):
    path = os.path.join("..","data","la_liga_events",name)
    f = open(path)
    data = json.load(f)
    return data

# Czyszczenie danych
# nadpisz 
#  True -  dane zostana dodane do pliku
#  False - plik zostanie nadpisanu
#
def uploadData(inputFile, outputFile, nadpisz):
  try:
    jsonFile = openGameFile(inputFile)
    clean_event = []
    for event in jsonFile:
      if(event["type"]["name"]=="Shot"):
        clean_event = []
        #time
        clean_event.append(event["minute"])
        #location
        clean_event.append(event["position"]["name"])
        # shot type
        clean_event.append(event["shot"]["type"]["name"])
        #outcome
        clean_event.append(event["shot"]["outcome"]["name"])
        
        clean_event.append(event["shot"]["technique"]["name"])

        m_players = []
        en_players = []
        keeper = []
        for player in event["shot"]["freeze_frame"]:
            player_p = 0
            if player["teammate"] :
                m_players.append(player["location"])
            elif player["position"] == 'Goalkeeper':
                keeper = player['location']
            else:
                en_players.append(player["location"])
        for i in range(0,10):
            if(len(m_players)>i):
              clean_event.append(m_players[i])
            else:
              clean_event.append("none")

        for i in range(0,9):
            if(len(en_players)> i):
              clean_event.append(en_players[i])
            else:
              clean_event.append("none")
              
        clean_event.append(keeper)

        if(event["shot"]["outcome"]=="Goal"):
          clean_event.append(1)
        else:
         clean_event.append(0)
  
     
  #Saving to a file
        with open(outputFile, 'a', newline='') as file:
          writer = csv.writer(file)
          writer.writerow(clean_event)

  except:
      raise Exception("invalid data")
for i in range(1,2):
    print(str(i) + "/100")
    name = " (" + str(i) + (").json")
    uploadData(name,"test_dataSet.csv",True)
1/100