I have csv data files with 2-line header, unknown number of rows and cols. For example
"x", "y"
"unit x", "unit y"
1, 2
3, 4
I use the following code to read the csv file and write it to a xls file
import csv, xlwt
f = open('example.csv', 'rb')
reader = csv.reader(f)
workbook = xlwt.Workbook()
sheet = workbook.add_sheet("Sheet 1")
for rowi, row in enumerate(reader):
for coli, value in enumerate(row):
sheet.write(rowi,coli,value)
workbook.save("example.xls")
The problem is it write all my data as text in xls. How can I convert text to number? I know first 2 row are header and data starts from row 3 to the end. How can I do this?
You can check to see if you are in row 3 onward and convert
valueto int.You could also try converting all values to numbers.