Python - 将 excel 序列日期转换为日期时间
本文将讨论在Python中将 excel 串行日期转换为 DateTime。
Excel“序列日期”格式实际上是从1900-01-00开始的天数,即1900年1月1日。例如excel序列日期编号43831代表2020年1月1日,将43831转换为DateTime后就变成了2020 -01-01。
通过使用xlrd.xldate_as_datetime()函数可以实现这一点。 xlrd.xldate_as_datetime()函数用于将 excel 日期/时间数字转换为 datetime.datetime 对象。
Syntax: xldate_as_datetime (xldate, datemode)
Parameters: This function accepts two parameters that are illustrated below:
- xldate: This is the specified excel date that will converted into datetime.
- datemode: This is the specified datemode in which conversion will be performed.
Return values: This function returns the datetime.datetime object.
首先,调用 xlrd.xldate_as_datetime(date, 0)函数将指定的 Excel 日期转换为 datetime.datetime 对象。然后,对返回的 datetime.datetime 对象调用 datetime.datetime.date()函数以将日期作为 datetime.date 对象返回。最后,调用 datetime.date.isoformat()函数将返回的 datetime.date 对象转换为 ISO 格式的日期字符串。
让我们看一些例子来说明上述算法:
示例:将excel序列日期转换为字符串日期的Python程序
Python3
# Python3 code to illustrate the conversion
# of excel serial date to datetime
# Importing xlrd module
import xlrd
# Initializing an excel serial date
xl_date = 43831
# Calling the xldate_as_datetime() function to
# convert the specified excel serial date into
# datetime.datetime object
datetime_date = xlrd.xldate_as_datetime(xl_date, 0)
# Calling the datetime_date.date() function to convert
# the above returned datetime.datetime object into
# datetime.date object
date_object = datetime_date.date()
# Calling the isoformat() function to convert the
# above returned datetime.date object into the
# ISO format date string
string_date = date_object.isoformat()
# Getting the converted date string as output
print(string_date)
# Getting the type of returned date format
print(type(string_date))
Python3
# Python3 code to illustrate the conversion
# of excel serial date to datetime
# Importing xlrd module
import xlrd
# Initializing an excel serial date
xl_date = 43831
# Calling the xldate_as_datetime() function to
# convert the specified excel serial date into
# datetime.datetime object
datetime_date = xlrd.xldate_as_datetime(xl_date, 0)
# Calling the datetime_date.date() function to convert
# the above returned datetime.datetime object into
# datetime.date object
date_object = datetime_date.date()
# Getting the converted date date as output
print(date_object)
# Getting the type of returned date format
print(type(date_object))
输出:
2020-01-01
示例2:将excel序列号转换为DateTime的Python程序
蟒蛇3
# Python3 code to illustrate the conversion
# of excel serial date to datetime
# Importing xlrd module
import xlrd
# Initializing an excel serial date
xl_date = 43831
# Calling the xldate_as_datetime() function to
# convert the specified excel serial date into
# datetime.datetime object
datetime_date = xlrd.xldate_as_datetime(xl_date, 0)
# Calling the datetime_date.date() function to convert
# the above returned datetime.datetime object into
# datetime.date object
date_object = datetime_date.date()
# Getting the converted date date as output
print(date_object)
# Getting the type of returned date format
print(type(date_object))
输出:
2020-01-01