Best way to inplace alter a list going into postgres
Sayth Renshaw
flebber.crue at gmail.com
Tue May 31 00:27:32 EDT 2016
Hi
What is the best way to inplace alter a list going into a postgres database using split but being sure that the order of items remains correct.
I am using this list of ids to retrieve data from XML
horseattrs = ('id', 'race_id', 'horse', 'number', 'finished', 'age', 'sex',
'blinkers', 'trainernumber', 'career', 'thistrack', 'firstup',
'secondup', 'variedweight', 'weight', 'pricestarting')
one example output looks like this.
['188745', '215639', 'Beau Tirage', '2', '0', '4', 'G', '0', '211', '11-3-1-4 $72390.00', '2-0-0-2 $8970.00', '3-2-0-0 $30085.00', '3-1-0-0 $15450.00', '59', '59', '']
so if I need to manage to split say a[10] if I call the list a and break that into 4 columns. Maybe like this.
In [1]: a = ['188745', '215639', 'Beau Tirage', '2', '0', '4', 'G', '0', '211', '11-3-1-4 $72390.00', '2-0-0-2 $8970.00', '3-2-0-0 $30085.00', '3-1-0-0 $15450.00', '59', '59', '']
In [2]: print(a)
['188745', '215639', 'Beau Tirage', '2', '0', '4', 'G', '0', '211', '11-3-1-4 $72390.00', '2-0-0-2 $8970.00', '3-2-0-0 $30085.00', '3-1-0-0 $15450.00', '59', '59', '']
In [3]: print(a[10])
2-0-0-2 $8970.00
In [6]: b = print(a[10].replace('-',' '))
2 0 0 2 $8970.00
In [9]: print(b.split(' ',))
['2', '0', '0', '2', '$8970.00']
Currently using this as the insert
conn = psycopg2.connect("")
with conn, conn.cursor() as cur:
# First, create tables.
cur.execute("drop table if exists meetings, races, horses")
cur.execute("create table meetings (" +
", ".join("%s varchar" % fld for fld in meetattrs)
+ ")")
cur.execute("create table races (" +
", ".join("%s varchar" % fld for fld in raceattrs)
+ ")")
cur.execute("create table horses (" +
", ".join("%s varchar" % fld for fld in horseattrs)
+ ")")
# Now walk the tree and insert data.
for filename in sorted(file_list):
for meeting in pq(filename=my_dir + filename):
meetdata = [meeting.get(attr) for attr in meetattrs]
cur.execute("insert into meetings values (" +
",".join(["%s"] * len(meetattrs)) + ")", meetdata)
for race in meeting.findall("race"):
race.set("meeting_id", meeting.get("id"))
racedata = [race.get(attr) for attr in raceattrs]
cur.execute("insert into races values (" +
",".join(["%s"] * len(raceattrs)) + ")", racedata)
for horse in race.findall("nomination"):
horse.set("race_id", race.get("id"))
horsedata = [horse.get(attr) for attr in horseattrs]
print(horsedata)
cur.execute("insert into horses values (" +
",".join(["%s"] * len(horseattrs)) + ")",
horsedata)
What is the best way to manage this.
Sayth
More information about the Python-list
mailing list