I am creating a script where I am reading from csv file and iterating all the rows in the csv file but i need help to generate insert statements of each row and I am also output it that into a file. So my only issue is the generate insert statements of each row. Thanks for the help.
here is my code:
import csv
openFile = open('data.csv', 'r')
csvFile = csv.reader(openFile)
header = next(csvFile)
headers = map((lambda x: '`' + x + '`'), header)
insert = 'INSERT INTO my_table (' + ", ".join(headers) + ") VALUES "
l = [*range(0, 59, 1)]
for row in csvFile:
values = list(row[i] for i in l)
print(values)
data = insert + "(" + ", ".join(values) + ");"
print(data)
createOnFile = open("data.txt","w+")
createOnFile.write(data)
createOnFile.close()
openFile.close()