-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathshare.py
More file actions
72 lines (58 loc) · 2.35 KB
/
share.py
File metadata and controls
72 lines (58 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import mysql.connector
import argparse
import configparser
import pandas as pd
import csv
import json
# parser to choose the database where the table will be written
parser = argparse.ArgumentParser()
parser.add_argument("-db", "--database", help = "Choices: dev1, dev2, testing, production", required=True)
parser.add_argument("-host", "--host", help = "Host choices: aws, localhost", required=True)
args = parser.parse_args()
input_db = args.database
input_host = args.host
config = configparser.ConfigParser()
config.read('helpers/config.ini')
host = config[input_host+"-"+input_db]['host']
user = config[input_host+"-"+input_db]['user']
passwd = config[input_host+"-"+input_db]['passwd']
database = config[input_host+"-"+input_db]['database']
conn = mysql.connector.connect(user=user, passwd=passwd, host=host, database=database)
cursor = conn.cursor()
CEventID = cursor.execute("SELECT eventID FROM events WHERE currentEvent = 1")
CEventID = cursor.fetchone()[0]
print("Running share.py")
query = "SELECT element FROM share WHERE share = 1"
cursor.execute(query)
elementList = (cursor.fetchall())
elements = str(elementList)
elements = elements.replace(",), (", ", ")
elements = elements.replace(elements[len(elements) - 3:], "")
elements = elements.replace(elements[0],"")
elements = elements.replace(elements[0],"")
elements = elements.translate(str.maketrans("", "", "'"))
query = f"SELECT {elements} FROM matchScouting WHERE eventID = {CEventID} AND teamMatchNum <= 12"
cursor.execute(query)
sharedData = cursor.fetchall()
headerList = []
for i in elementList:
i = str(i).translate(str.maketrans("", "", "(),")) # Convert tuple to string before calling translate
headerList.append(i)
with open('195scoutingData.csv', mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(headerList)
for row in sharedData:
writer.writerow(row)
data = []
with open('195scoutingData.csv', 'r') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
data.append(row)
with open('195scoutingData.json', 'w') as jsonfile:
json.dump(data, jsonfile, indent=4)
# df.to_csv (r'195scoutingData.csv', header=headerList, index = False) # place 'r' before the path name
# df = pd.read_csv('195scoutingData.csv')
# df.to_json(r'195scoutingData.json', orient='records', lines=True)
print("share.py complete")
cursor.close()
conn.close()