Python Forum
Phyton code to load a comma separated csv file in to a dict and then in to a dB
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Phyton code to load a comma separated csv file in to a dict and then in to a dB
#1
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
Reply
#2
what have you tried?
Reply
#3
(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))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Phyton Opening files on windows C: pc EddieG 3 984 Mar-29-2023, 03:19 PM
Last Post: buran
  How can histogram bins be separated and reduce number of labels printed on x-axis? cadena 1 882 Sep-07-2022, 09:47 AM
Last Post: Larz60+
  [SOLVED] [BeautifulSoup] Turn select() into comma-separated string? Winfried 0 1,112 Aug-19-2022, 08:07 PM
Last Post: Winfried
  How to load all pages of the pdf file to the program alicenguyen 9 2,250 Jul-04-2022, 03:14 AM
Last Post: alicenguyen
  How to format Excel column with comma? dee 0 1,360 Jun-13-2022, 10:11 PM
Last Post: dee
  decimal comma DPaul 9 2,268 Feb-22-2022, 12:25 PM
Last Post: DeaD_EyE
Exclamation Phyton Metin2 Game Left Click not working. Help anonym35 0 1,530 Feb-02-2022, 07:01 PM
Last Post: anonym35
  Adding a comma in the resulting value stsxbel 6 2,637 May-22-2021, 09:24 PM
Last Post: stsxbel
  How to instantly add quotation marks and comma for parameters? cheers100 4 8,049 Oct-22-2020, 12:51 PM
Last Post: cheers100
  print scripts example includes comma that seems to serve no purpose flour_power_33 5 2,795 Sep-02-2020, 03:32 AM
Last Post: flour_power_33

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020