Having a .xls with the following structure
Matching Fields Date Person
1 2015-02-10 Bob
2 2015-01-27 Billy
I'm trying to convert it to .csv and I was having problems converting the dates, because for example the 2015-02-10
is saved as an float in the csv, not with the date format.
In order to save the date correctly, this is my code:
# -*- coding: utf-8 -*-
import xlrd
import csv
def csv_from_excel():
wb = xlrd.open_workbook('C:/myfile.xls', encoding_override="cp1252")
sh = wb.sheet_by_name('Sheet1')
your_csv_file = open('C:/output.csv', 'wb')
wr = csv.writer(your_csv_file, quoting=csv.QUOTE_ALL)
for rownum in xrange(sh.nrows):
print sh.row_values(rownum)[1]
date = sh.row_values(rownum)[1]
if isinstance( date, float) or isinstance( date, int ):
year, month, day, hour, minute, sec = xlrd.xldate_as_tuple(date,0)
py_date = "%04d-%02d-%02d" % (year, month, day)
wr.writerow([py_date] + [unicode(val).encode('utf8') for val in sh.row_values(rownum)][1:])
else:
wr.writerow([unicode(val).encode('utf8') for val in sh.row_values(rownum)])
your_csv_file.close()
csv_from_excel()
The output I get is:
"Matching Fields","Date","Person"
"2015-02-10","42031.0","Bob"
"2015-01-27","42031.0","Billy"
As you can see here are two problems:
- The first column, the values of
Matching Fields
,"1"
and"2"
are not showing. - The "float format" of the date is still showing.
How can I solve both problems in order to get my desired output?
"Matching Fields","Date","Person"
"1","2015-02-10","Bob"
"2","2015-01-27","Billy"
PS: The reason of using [unicode(val).encode('utf8') for val in sh.row_values(rownum)]
instead of just sh.row_values(rownum)
is because I've some russian characters on my csv (which are not appearing on this example), so I need to convert each to utf8
with that code.