如何以行而不是字符串显示数据?

时间:2017-11-24 17:32:52

标签: python

现在,当我打印gameuniqueteams时,它显示为一个字符串。在SQL上,每行添加一个新团队,同时我希望每个团队单独显示。在这个阶段,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 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()

2 个答案:

答案 0 :(得分:0)

以下是您提出的问题的答案:

import pprint

teams = ['Arsenal', 'Bournemouth', 'Brighton', 'Burnley', 'Chelsea']
for team in teams:
    print([team])

teams1 = [[team]
          for team in teams[:3]]
print(teams1)
pprint.pprint(teams1, width=1)
pprint.pprint(teams, width=1)

['Arsenal']
['Bournemouth']
['Brighton']
['Burnley']
['Chelsea']
[['Arsenal'], ['Bournemouth'], ['Brighton']]
[['Arsenal'],
 ['Bournemouth'],
 ['Brighton']]
['Arsenal',
 'Bournemouth',
 'Brighton',
 'Burnley',
 'Chelsea']

对我而言,这与您的数据库代码有何关系并不明显。 看起来您的allteams分配缩进太多了 - 您希望在读取所有CSV行之后发生后续处理。也许您想要迭代for team in sorted(uniqueteams):,然后data1 = (team,),并将 存储为数据库行。

这是一个数据结构,您可以从中了解:set。

unique_teams = set(['Bournemouth', 'Brighton', 'Brighton'])
unique_teams.add('Burnley')
print(unique_teams)


{'Bournemouth', 'Burnley', 'Brighton'}

该套装将为您提供独特性,无需重复成员资格查询。

答案 1 :(得分:0)

您的问题似乎与数据库或文件无关,而只是在打印时使用字符串操作。

一些列表

foo = ["some", "words", "go", "here"]
当您通过换行符加入

时,

会成为行

>>> "\n".join(foo)
"""some
words
go
here"""

这些引号只是Python的标记,因此打印它会使它们消失。

>>> print("\n".join(foo))
some
words
go
here

我认为你的数据结构作为一个列表是正确的,你应该只是操纵那个字符串列表来获得你正在寻找的输出。