I need to convert excel to csv. But i can't multi write the csv file
import sys,os
sys.path.insert(0,'D:/apera/Python27/xlrd-0.9.3')
import xlrd
import csv
path = "D:/apera/Workspace/Sounding"
CSVFile1 = "D:/apera/Workspace/Sounding/sounding001.csv"
CSVFile2 = "D:/apera/Workspace/Sounding/sounding002.csv"
for root,dirs,files in os.walk(path):
xlsfiles=[ _ for _ in files if _.endswith('.xls') ]
for xlsfile in xlsfiles:
wb = xlrd.open_workbook(os.path.join(root,xlsfile))
n = len(wb.sheets())
ws = wb.sheet_by_name("INPUT")
with open(CSVFile1, 'wb') as csvfile:
wr = csv.writer(csvfile, quoting=csv.QUOTE_ALL, delimiter=';')
for rownum in xrange(ws.nrows):
wr.writerow(list(x.encode('latin1')for x in ws.row_values(rownum)))
with open(CSVFile2, 'wb') as csvfile:
wr = csv.writer(csvfile, quoting=csv.QUOTE_ALL, delimiter=';')
for rownum in xrange(ws.nrows):
wr.writerow(list(x.encode('latin1')for x in ws.row_values(rownum)))
csvfile.close()
This the edited question. I need to write the csv files. This is only 2 files that needs to write. So I write it two times. So is there a way to make it simpler. So I don't need to with open csvfile3, 4, 5 and so on.. Thanks
pandas.ExcelFileobject, and then useDataFrame.to_csv()function to write to csv. Much easier I'd say.