अभी, जब मैं प्रिंट gameuniqueteams
यह एक स्ट्रिंग के रूप को दर्शाता है। एसक्यूएल पर, प्रत्येक पंक्ति में एक नई टीम जोड़ा जाता है, जबकि मैं प्रत्येक अलग-अलग दिखाना चाहते हैं। इस स्तर पर, gameuniqueteams निम्न स्ट्रिंग दिखाएगा
['Arsenal', 'Bournemouth', 'Brighton', 'Burnley', 'Chelsea']
मैं इतना है कि जब मैं यह प्रत्येक टीम से पता चलता है SQL के एक पंक्ति में अपने आप में हस्तांतरण यह पंक्ति प्रति दिखाना चाहते हैं।
['Arsenal']
['Bournemouth']
['Brighton']
['Burnley']
['Chelsea']
इस मामले में यह मदद करता है में अपने पूरे कोड है! मुझे क्या करना चाहिए?
#!/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 IGNORE 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()