Writing Output of Python Program to .txt File -


i've written program reads csv file , outputs contents insert statements. wrote execute program should take output of csv parser program , write .txt file instead of writing entire output writes first statement.

here code executor:

import sys  open('insert.txt', 'wb') f:     subprocess.check_call(["python", "csvparsermultiple.py"], stdout=f) 

and code parser:

import csv, os  path = 'c:/users/user/desktop/test/' 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 testing (' + "'id', 'vehicleid', " + ', '.join(columnheaders) + ') values '         row in csvfile:             values = map((lambda x: "'" + x.strip() + "'"), row[:-1])             print (insert + "(" + ", ".join(values) + ");")         openfile.close() 

i'm not entirely sure makes sense have them 2 separate programs not them run defined functions in same program life of me. how can execution program output lines of parser program instead of single line? , how can combine them 1 program?

you making things more complicated need be. nest open statements using with. 1 program. , prints screen , writes file.

import csv, os  path = 'c:/users/user/desktop/test/' file in os.listdir(path):     if file.endswith('.csv'):         # print ('parsing file: ' + os.path.basename(path + file))         open(path + file) infile:             open(path+file+".txt",'w') outfile:                 csvfile = csv.reader(infile)                 getheader = next(csvfile)                 columnheaders = map((lambda x: "'" + x + "'"), getheader[:-1])                 insert = 'insert testing (' + "'id', 'vehicleid', " + ', '.join(columnheaders) + ') values '                 row in csvfile:                     values = map((lambda x: "'" + x.strip() + "'"), row[:-1])                     print (insert + "(" + ", ".join(values) + ");")                     outfile.write(insert + "(" + ", ".join(values) + ");" + "\n") 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -