xlrd——读取日期格式数据
原文:https://www.cnblogs.com/rongge95500/p/11655322.html
经常使用python操作Excel,就会遇到各种坑,比如,有时候你读取到的某一单元格的数据,你预想的结果本来应该是这样的 ['2019-03-26', '2019-03-26'...],但是它却是这样的[43550.651550925926, 43550.6691087963,...],真是很蛋疼。于是你会找各种解决办法,去解决这个问题!所以鄙人共找到了这么几种方法,仅供参考。
这些方法都会使用到这么一个包:xlrd,一般都是python自带的
导包
import datetime
import xlrd
|
方法一:自己拼接日期(个人不建议这么做)
def date_as_join(raw_date):
'''
用xlrd模块读取日期格式的单元格
:param raw_date 原始数据,list类型,如:[52234.0, 798798.0]
:return: 日期列表 date_list
'''
date_list = []
for date in raw_date:
new_date = xlrd.xldate_as_tuple(date, 0 )
date_str = '-' .join([ str (i) for i in new_date[: - 3 ]])
format_date = datetime.datetime.strptime(date_str, '%Y-%m-%d' ).strftime( '%Y-%m-%d' )
date_list.append(format_date)
print (date_list)
return date_list
date_as_join([ 3422.0 , 8675.0 ])
|
方法二:使用 xldate_as_datetime
def date_as_datetime(raw_date):
'''
用xlrd模块读取日期格式的单元格
:param raw_date 原始数据,list类型,如:[52234.0, 798798.0]
:return: 日期列表 date_list
'''
date_list = []
for date in raw_date:
new_date = xlrd.xldate_as_datetime(date, 0 ).strftime( '%m-%d' )
date_list.append(new_date)
print (date_list)
return date_list
date_as_datetime([ 3422.0 , 867575.0 ])
|
方法三:使用 xldate_as_tuple
def date_as_tuple(raw_date):
'''
用xlrd模块读取日期格式的单元格
:param raw_date 原始数据,list类型,如:[52234.0, 798798.0]
:return: 日期列表 date_list
'''
date_list = []
for date in raw_date:
new_date = datetime.datetime( * xlrd.xldate_as_tuple(date, 0 )).strftime( '%Y-%m-%d' )
date_list.append(new_date)
print (date_list)
return date_list
date_as_tuple([ 34242.0 , 22342.0 ])
|
经常使用python操作Excel,就会遇到各种坑,比如,有时候你读取到的某一单元格的数据,你预想的结果本来应该是这样的 ['2019-03-26', '2019-03-26'...],但是它却是这样的[43550.651550925926, 43550.6691087963,...],真是很蛋疼。于是你会找各种解决办法,去解决这个问题!所以鄙人共找到了这么几种方法,仅供参考。
这些方法都会使用到这么一个包:xlrd,一般都是python自带的
导包
import datetime
import xlrd
|
方法一:自己拼接日期(个人不建议这么做)
def date_as_join(raw_date):
'''
用xlrd模块读取日期格式的单元格
:param raw_date 原始数据,list类型,如:[52234.0, 798798.0]
:return: 日期列表 date_list
'''
date_list = []
for date in raw_date:
new_date = xlrd.xldate_as_tuple(date, 0 )
date_str = '-' .join([ str (i) for i in new_date[: - 3 ]])
format_date = datetime.datetime.strptime(date_str, '%Y-%m-%d' ).strftime( '%Y-%m-%d' )
date_list.append(format_date)
print (date_list)
return date_list
date_as_join([ 3422.0 , 8675.0 ])
|
方法二:使用 xldate_as_datetime
def date_as_datetime(raw_date):
'''
用xlrd模块读取日期格式的单元格
:param raw_date 原始数据,list类型,如:[52234.0, 798798.0]
:return: 日期列表 date_list
'''
date_list = []
for date in raw_date:
new_date = xlrd.xldate_as_datetime(date, 0 ).strftime( '%m-%d' )
date_list.append(new_date)
print (date_list)
return date_list
date_as_datetime([ 3422.0 , 867575.0 ])
|
方法三:使用 xldate_as_tuple
def date_as_tuple(raw_date):
'''
用xlrd模块读取日期格式的单元格
:param raw_date 原始数据,list类型,如:[52234.0, 798798.0]
:return: 日期列表 date_list
'''
date_list = []
for date in raw_date:
new_date = datetime.datetime( * xlrd.xldate_as_tuple(date, 0 )).strftime( '%Y-%m-%d' )
date_list.append(new_date)
print (date_list)
return date_list
date_as_tuple([ 34242.0 , 22342.0 ])
|