I've written a program that reads a CSV file and outputs the contents as insert statements. I then wrote an execute program that should take the output of the CSV parser program and write it to a .txt file but instead of writing the entire output it only writes the first statement.
Here is the code for the executor:
import sys
with open('insert.txt', 'wb') as f:
subprocess.check_call(["python", "CSVParserMultiple.py"], stdout=f)
And the code for the parser:
import csv, os
path = 'C:/Users/user/Desktop/test/'
for file in os.listdir(path):
if file.endswith('.csv'):
# print ('Parsing file: ' + os.path.basename(path + file))
openFile = open(path + file)
csvFile = csv.reader(openFile)
getHeader = next(csvFile)
columnHeaders = map((lambda x: "'" + x + "'"), getHeader[:-1])
insert = 'INSERT INTO testing (' + "'ID', 'VehicleID', " + ', '.join(columnHeaders) + ') VALUES '
for row in csvFile:
values = map((lambda x: "'" + x.strip() + "'"), row[:-1])
print (insert + "(" + ", ".join(values) + ");")
openFile.close()
I'm not entirely sure that it makes sense to have them as two separate programs but I could not get them to run as defined functions in the same program for the life of me. How can I get the execution program to output all the lines of the parser program instead of just a single line? And how can I combine them into one program?