Python:xlrd从浮标中挑选日期 [英] Python: xlrd discerning dates from floats

查看:369
本文介绍了Python:xlrd从浮标中挑选日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Python上使用xlrd导入包含文本,数字和日期的文件。



我尝试过如下:

 如果工作表中的/.cell_value:
do_this
其他:
do_that
但是这没有用,因为后者发现日期被存储为浮点数,而不是字符串。要将它们转换为datetime类型,我做了:

  try:
get_row = str(datetime.datetime(* xlrd。 xldate_as_tuple(worksheet.cell_value(i,col - 1),workbook.datemode))
除了:
get_row = unicode(worksheet.cell_value(i,col - 1))

当单元格包含文本时,我有一个例外。现在我想把数字作为数字和日期作为日期,因为现在所有的数字都转换成日期。



任何想法?

$ b $嗯,没关系,我找到了一个解决方案,这里就是!

  try:
cell = worksheet.cell(row - 1,i)
如果cell.ctype == xlrd.XL_CELL_DATE:
date = datetime.datetime(1899, 12,30)
get_ = datetime.timedelta(int(worksheet.cell_value(row - 1,i)))
get_col2 = str(date + get _)[:10]
d = datetime .datetime.strptime(get_col2,'%Y-%m-%d')
get_col = d.strftime('%d-%m-%Y')
else:
get_col = unicode(int(worksheet.cell_value(row-1,i)))
除了:
get_col = unicode(worksheet.cell_value(row - 1,i))

有一点解释:事实证明,使用xlrd你可以检查一个单元格的类型,并检查它是否是一个日期。此外,Excel似乎有一种奇怪的方式来节省一天的时间。它将它们保存为浮点数(左侧部分为几天,右半部分为数小时),然后需要一个特定的日期(1899,12,30,似乎可以正常工作),并从浮点数添加日期和时间以创建日期。所以,要创建我想要的日期,我只是添加他们,只保留10个第一个字母([:10])来摆脱时间(00.00.00或某事...)。我也改变了days_months-years的顺序,因为在希腊我们使用了不同的顺序。最后,这个代码还会检查它是否可以将一个数字转换成一个整数(我不希望任何浮点数显示在我的程序...),如果一切都失败,它只是使用单元格(如果有的话)单元格中的字符串...)。
我希望你觉得有用,我想还有其他线程说这是不可能的或某事...


I wanted to import a file containing text, numbers and dates using xlrd on Python.

I tried something like:

if "/" in worksheet.cell_value:
    do_this
else:
    do_that  

But that was of no use as I latter discovered dates are stored as floats, not strings. To convert them to datetime type I did:

try:
    get_row = str(datetime.datetime(*xlrd.xldate_as_tuple(worksheet.cell_value(i, col - 1), workbook.datemode)))
except:
    get_row = unicode(worksheet.cell_value(i, col - 1))

I have an exception in place for when the cell contains text. Now i want to get the numbers as numbers and the dates as dates, because right now all numbers are converted to dates.

Any ideas?

解决方案

Well, never mind, I found a solution and here it is!

try:
    cell = worksheet.cell(row - 1, i)
    if cell.ctype == xlrd.XL_CELL_DATE:
        date = datetime.datetime(1899, 12, 30)
        get_ = datetime.timedelta(int(worksheet.cell_value(row - 1, i)))
        get_col2 = str(date + get_)[:10]
        d = datetime.datetime.strptime(get_col2, '%Y-%m-%d')
        get_col = d.strftime('%d-%m-%Y')
    else:
        get_col = unicode(int(worksheet.cell_value(row - 1, i))) 
except:
    get_col = unicode(worksheet.cell_value(row - 1, i))

A bit of explanation: it turns out that with xlrd you can actually check the type of a cell and check if it's a date or not. Also, Excel seems to have a strange way to save daytimes. It saves them as floats (left part for days, right part for hours) and then it takes a specific date (1899, 12, 30, seems to work OK) and adds the days and hours from the float to create the date. So, to create the date that I wanted, I just added them them and kept only the 10 first letters ([:10]) to get rid of the hours(00.00.00 or something...). I also changed the order of days_months-years because in Greece we use a different order. Finally, this code also checks if it can convert a number to an integer(I don't want any floats to show at my program...) and if everything fails, it just uses the cell as it is(in cases there are strings in the cells...). I hope that you find that useful, I think there are other threads that say that this is impossible or something...

这篇关于Python:xlrd从浮标中挑选日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆