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 8826629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:11:10+00:00 2026-06-14T07:11:10+00:00

I am running into an issue where I have some data in a .xls

  • 0

I am running into an issue where I have some data in a .xls file(example below).

  A            B           C         D         E        F
John Smith     8:00AM      9:00AM    10:00AM    5:00PM  8.00

When I write it to a csv using the Python CSV module it comes out as

John,Smith,0.333333333,0.375,0.416666667,0.708333333,0.333333333

Now the interesting part is if I manually save the xls file as a MSDOS csv I get the desired output of

John,Smith,8:00 AM,9:00 AM,10:00 AM,5:00 PM,8:00

Here is the function I am running. Its a bit messy so I apologize in advance.

def csv_gen(filepath, saveto):
    for files in glob.glob("*.xls"):
        shutil.copy(filepath + "\\" + files, saveto)
        with xlrd.open_workbook(files) as wb:
            sh = wb.sheet_by_index(0)
            newfile = saveto + files[:-4] + '.csv'
            now = datetime.datetime.now()
            dates = now.strftime("%m-%d-%Y")
            filestart = [saveto + files]
            time = [dates]
            with open(newfile, 'wb') as f:
                c = csv.writer(f,delimiter=',')
                list =  range(sh.nrows)
                last = range(sh.nrows)[-1]
                list.remove(0)
                list.remove(3)
                list.remove(2)
                list.remove(1)
                list.remove(last)
                #Iterate through data and show values of the rows
                for r in list:
                    lines = sh.row_values(r)
                    del lines[:4]
                    stuff = lines + filestart + time
                    #Remove blanks so csv doesnt have uneeded data
                    if lines[0] is '':
                        del stuff[:]
                    #Write to csv file with new data
                    if any(field.strip() for field in stuff):
                        c.writerow(stuff)
            shutil.move(newfile, mergeloc)

I don’t understand why this is coming out this way. I have tried adding the dialect flag to the csv writer to be ‘excel’, but the output is still the same.

Update:

If I save the document as a csv as so workBook.SaveAs(test.csv, 24) The encoding 24 is for MSDOS. I get the desired output of

John,Smith,8:00 AM,9:00 AM,10:00 AM,5:00 PM,8:00

But when the csv module grabs it and removes some blank rows and deletes a few things at the end it writes the rows out and that is when I get the decimals again

John,Smith,0.333333333,0.375,0.416666667,0.708333333,0.333333333

The purpose of the csv module is to modify rows and delete blank rows.

Update

 for r in list: 
     cells = sh.row_values(r) 
     csv_row = cells[0] for col_value in cells[1:]:
         csv_row.append(datetime.time(*xlrd.xldate_as_tuple(col_value, 0)[3:])) 

Added the row_values to just return the value of the cell and not xldata:0.33333. Then added a * to make the pass a positional argument.

  • 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-14T07:11:12+00:00Added an answer on June 14, 2026 at 7:11 am

    That doesn’t look like a problem in csv module to me, it looks like something is going wrong in reading the .xls file.

    According to the xlrd docs dates in Excel worksheets are a pretty awful mess

    Dates in Excel spreadsheets

    In reality, there are no such things. What you have are floating point
    numbers and pious hope. There are several problems with Excel dates:

    I did a quick test with a new .xls file with the contents you provided in there. Python has no problems reading the file, although I don’t have Excel on my machine, I made the file in LibreOffice and saved it as .xls. Even so, the fields come out as unicode strings on the python side.

    You should be able to use the xlrd.xldate_as_tuple(xldate, datemode) (link) to convert the float into a python date tuple. Doing

    print xlrd.xldate_as_tuple(0.333333333,0)
    

    prints out

    (0, 0, 0, 8, 0, 0)
    

    UPDATE

    So you probably want something like the following, changing the for loop that goes over your rows

    ...
    for r_idx in list:
        cells = sh.row(r)
        csv_row = [cells[0]] # the first row value should be ok as just a string
        for col_value in cells[1:]:
            # add the date time column values to the converted csv row
            csv_row.append( datetime.time(xlrd.xldate_as_tuple(col_value, 0)[3:]) )
        ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm running into an issue where webrat can't see checkboxes. I have some code
I am running into some performance issue with php and mysql page. i have
I have been using this code, but have been running into some memory issues:
I'm running into an issue in that I have a document indexed with elasticsearch
I'm using C# and WebForms and running into an issue. I have a class
I have extended SimpleCursorAdapter and am running into a weird issue with bindView, which
We're running into transaction issue with Grails. During performance test we have a scenario,
I'm running into a crash issue while developing an iPhone app with Core Data.
I'm putting some documentation together using Bootstrap and running into an issue with the
I'm running into this strange issue. Basically I have a loop that loops forever,

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.