繁体   English   中英

csv模块将时间写为十进制

[英]csv module writes time as decimal

我遇到了一个问题,我在.xls文件中有一些数据(下面的例子)。

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

当我使用Python CSV模块将它写入csv时,它就像是

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

现在有趣的部分是如果我手动将xls文件保存为MSDOS csv我得到了所需的输出

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

这是我正在运行的功能。 它有点乱,所以我提前道歉。

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)

我不明白为什么会出现这种情况。 我已经尝试将方言标志添加到csv编写器为'excel',但输出仍然是相同的。

更新:

如果我将文档保存为csv,那么workBook.SaveAs(test.csv, 24)编码24用于MSDOS。 我得到了所需的输出

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

但是当csv模块抓取它并删除一些空行并在最后删除一些东西时它会将行写出来,那时我再次得到小数

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

csv模块的目的是修改行并删除空行。

更新

 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:])) 

添加了row_values只返回单元格的值而不是xldata:0.33333。 然后添加一个*以使传递成为位置参数。

对我来说这看起来不像csv模块中的问题,看起来在读取.xls文件时出现了问题。

根据Excel中的xlrd docs日期,工作表是一个非常糟糕的混乱

Excel电子表格中的日期

实际上,没有这样的东西。 你有什么是浮点数和虔诚的希望。 Excel日期有几个问题:

我用一个新的.xls文件快速测试了你在那里提供的内容。 Python在读取文件时没有问题,虽然我的机器上没有Excel,但我在LibreOffice中创建了文件并将其保存为.xls。 即便如此,字段在python端作为unicode字符串出现。

您应该能够使用xlrd.xldate_as_tuple(xldate, datemode)链接 )将float转换为python日期元组。

print xlrd.xldate_as_tuple(0.333333333,0)

打印出来

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

UPDATE

所以你可能想要类似下面这样的东西,改变遍历你的行的for循环

...
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:]) )
    ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM