How to write the retrieved DB from MS SQL server into new CSV File with headers using python 2.7.6 -
i trying view database retrieved ms sql server in csv file using python headers(column names)and without braces , quotes. code follows:
import csv import pyodbc outpath="path\\test1.csv" output = open(outpath, "w") cnxn = pyodbc.connect('driver={sql server};server=sipldt0115;database=first_eg;uid=sa;pwd=wisdom') cursor = cnxn.cursor() sql = "select * first1" cursor.execute(sql) rows = cursor.fetchall() desc = cursor.description header = (desc[0][0], desc[1][0], desc[2][0], desc[3][0], desc[4][0]) print "%s %3s %s %3s %3s" % header row in rows: print row value = str(row).strip('(') output.write(str(value.replace(')','\n'))) output.close() f = open("path\\test1.csv").read() print f
output:
f_name l_name s_id branch course ('jash', 'u', 123, 'c', 'b') ('jash', 'u', 123, 'c', 'b') ('jash', 'u', 123, 'c', 'b') 'jash', 'u', 123, 'c', 'b' 'jash', 'u', 123, 'c', 'b' 'jash', 'u', 123, 'c', 'b'
in csv file, coming without headers. want view database table in csv file with header. how? possible? please reply yar!!
Comments
Post a Comment