Right now, when I print gameuniqueteams
it shows as a string. On SQL, at each row a new team gets added while I want each to show individually. At this stage, gameuniqueteams will show the following string
['Arsenal', 'Bournemouth', 'Brighton', 'Burnley', 'Chelsea']
I want it to show per row so that when I transfer it to sql each team shows in a row by itself.
['Arsenal']
['Bournemouth']
['Brighton']
['Burnley']
['Chelsea']
This is my entire code in case it helps! What should I do?
#!/usr/bin/python
# -*- coding: utf-8 -*-
import psycopg2
import sys
import csv
from itertools import count, cycle
from _tkinter import create
from setuptools.dist import sequence
from email.policy import default
path = r'C:\Users\sammy\Downloads\E0.csv'
with open(path, "r") as csvfile:
readCSV = csv.reader(csvfile, delimiter=",")
firstline = 1
con = None
con = psycopg2.connect("host='localhost' dbname='football' user='postgres' password='XXX'")
cur = con.cursor()
cur.execute("DROP TABLE teams")
cur.execute("CREATE TABLE teams (HomeTeamID SERIAL PRIMARY KEY, AllTeams123 VARCHAR)")
hometeams = []
awayteams = []
uniqueteams = []
allteams = []
gameuniqueteams = []
try:
for row in readCSV:
if firstline:
firstline=0
continue
HomeTeam = row[2]
AwayTeam = row[3]
hometeams.append(HomeTeam)
awayteams.append(AwayTeam)
allteams = hometeams + awayteams
for x in allteams:
if x not in uniqueteams:
uniqueteams.append(x)
gameuniqueteams = sorted(uniqueteams)
for x in gameuniqueteams:
print (x)
gameuniqueteams = (x)
data1 = (gameuniqueteams,)
query1 = "INSERT INTO teams (AllTeams123) VALUES (%s);"
cursor = con.cursor()
cursor.execute(query1, data1)
except psycopg2.DatabaseError as e:
if con:
con.rollback()
print ("Error %s % e", e)
sys.exit(1)
finally:
if con:
con.commit()
con.close()