import csv
import sys,os
import MySQLdb
def read_csv(filename):
with open(filename) as f:
f_csv = csv.reader(f)
headers = next(f_csv)
return headers
def conn_to_psto():
onn = MySQLdb.connect(database="testdb", \
user="postgres", \
password="pass123",\
host="127.0.0.1",\
port="5432")
cur = conn.cursor()
return cur
if __name__ == "__main__":
filename=sys.argv[1]
f_csv=read_csv(filename)
cur=conn_to_psto()
for row in f_csv:
field1=row[1]
...
sql='''insert into .......(field1,field2,...) values(%,%,...) '''.format(field1=field1,field2=field2.........))
cur.execute(sql)
conn.commit()
conn.close()