Python Forum

Full Version: Phyton code to load a comma separated csv file in to a dict and then in to a dB
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

Hope you are well.

Looking to do the followoing steps:

1. load a comma separated csv file in to a named python dictionary converting numbers that appear as strings in to integers

2. Once the data is in the dictionary load the data out to a mySQL db table

3. Load the data back in to Python in to a second dictionary keeping the integers as integers

Note the first column in the csv is a unique integer value called ID

Regards,
Mrsenorcheck
what have you tried?
(Nov-28-2019, 09:43 PM)Larz60+ Wrote: [ -> ]what have you tried?

Thank you for your reply.

Reading in the csv to a dictionary is easy enough, see below what I have, the hard part for me is then getting data in and out of a table mySQL.

I am guessing this is straight forward?

with open(r"C:\Users\oconnaid\Desktop\AIT\ASL Notes\Assignment\Historic_PL.csv") as data_file:
# read in the first line containing the headers
headers = data_file.readline()

# for each other line in the file
for line in data_file:
# split each line into components (remove white space from ends of line)
Pos,Club,Seasons,Pld,Win,Draw,Loss,GF,GA,GD,Pts,First,Second,Third,Fourth,Relegated,Best = line.strip().split(",")

# insert the data into the dictionary
premier[(int(Pos), Club, int(Seasons), int(Pld), int(Win), int(Draw), int(Loss), int(GF), int(GA), int(GD), int(Pts), int(First), int(Second), int(Third), int(Fourth), int(Relegated))] = (int(Best))