Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9115055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:20:26+00:00 2026-06-17T04:20:26+00:00

I have csv data files with 2-line header, unknown number of rows and cols.

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:20:27+00:00Added an answer on June 17, 2026 at 4:20 am

    You can check to see if you are in row 3 onward and convert value to int.

    for rowi, row in enumerate(reader):
        for coli, value in enumerate(row):
            if rowi >= 2:
                value = int(value)
            sheet.write(rowi, coli, value)
    

    You could also try converting all values to numbers.

    for rowi, row in enumerate(reader):
        for coli, value in enumerate(row):
            try:
                value = int(value)
            except ValueError as e:
                pass
            sheet.write(rowi, coli, value)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got following file: file.csv header:2013/01/01, shasum: 495629218484151218892233214 content:data,a,s,d,f,g,h,j,k,l content:data,q,w,e,r,t,y,u,i,o,p content:data,z,x,c,v,b,n,m footer:2013/01/01 EOF
I have an xml schema and csv data to generate corresponding xml files. I
I have a number of images and a CSV data file that I want
I have a csv file with the following structure : The first line is
I have some csv files, I need to open csv file, read first line
So I have this app that processes CSV files. I have a line of
I have the following PHP script which reads from two CSV files, At the
I have a few csv files that contain strings with line breaks in them.
I have a large collection of data in an excel file (and csv files).
I have a large CSV data file -- ~1,444,000 rows of data -- that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.