Python extracting data from CSV and inserting into MYSQL database

Thread Starter

zazas321

Joined Nov 29, 2015
627
Hey. I am trying to upload data to my MYSQL table from the CSV file. My mysql table looks like:

2020-07-31-092857_1920x1080_scrot.png

and my CSV is as following:

2020-07-31-093139_1920x1080_scrot.png
I am not very experienced with python, but I have tried a few example codes:
Code:
import pandas as pd
import mysql.connector
hostname = 'localhost'
username = 'root'
password = 'pswd'
database = 'test'

myConnection = mysql.connector.connect( host=hostname, user=username, passwd=password, db=database )
#cur = myConnection.cursor()

data = pd.read_csv("rut1.csv")
print(data)
for row in data:
    print(row)
    #print(row[1])
    #print(row[2])
    
    #myConnection = mysql.connector.connect( host=hostname, user=username, passwd=password, db=database )
    #cur = myConnection.cursor()
    #cur.execute("REPLACE INTO komplektacija(Item,Serial,Quantity) VALUES(%s, %s, %i)",row)
    #cur.execute("REPLACE INTO komplektacija(Item,Serial,Quantity) VALUES(%s, %s, %i);" % (row[],row[2],row[3]))
    #myConnection.commit()
    #cur.close()
First of all, I do not think I properly understand how pandas.read_csv works as in how does it return data?
Code:
data = pd.read_csv("rut1.csv")
print(data)
returns me what I expect:
2020-07-31-093347_1920x1080_scrot.png
However,
Code:
for row in data:
      print(row)
returns me:
Item
Serial
Quantity
which is strange, since It outputs me a collum names, instead of rows. Could someone give me some hints here?
 

Attachments

upand_at_them

Joined May 15, 2010
458
This Python program should be helpful:

Code:
import pandas as pd

data = pd.read_csv("rut1.csv")
print(data)
print
print
for index, row in data.iterrows():
    print(row)
    print
    print(row[0])
    print(row[1])
    print(row[2])
    print
 
Top