[英]Error when combining datetime.date object with datetime.time object
I am currently trying to combine a datetime.date object with a datetime.time object to get the resulting datetime.datetime style object. 我目前正在尝试将datetime.date对象与datetime.time对象结合起来,以获取生成的datetime.datetime样式对象。
I run an SQL query and parse the results by doing the following: 我运行SQL查询并通过执行以下操作来解析结果:
import sqlite3
import time
import datetime
conn = sqlite3.connect('stu.sqlite3', detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
cur = conn.cursor()
cur.execute('SELECT intra_day.date, intra_day.time, intra_day.price, closing_price.date, closing_price.cprice FROM intra_day, closing_price WHERE intra_day.date = closing_price.date')
for row in cur:
date_intra, time_intra, price_intra, cp_date, cprice = (row[0], row[1], float(row[2]), row[3], float(row[4]))
date_intra = datetime.datetime.strptime(date_intra, '%d/%m/%Y %H:%M:%S').date()
time_intra = datetime.datetime.strptime(time_intra, '%H:%M:%S').time()
cp_date = datetime.datetime.strptime(cp_date, '%d/%m/%Y %H:%M:%S').date()
Once that has run, and I run the type and print tests in the IDLE i get the following: 一旦运行,然后在IDLE中运行类型和打印测试,我将得到以下信息:
type(date_intra)
Out[2]: datetime.date
type(time_intra)
Out[3]: datetime.time
type(cp_date)
Out[4]: datetime.date
print date_intra, time_intra, price_intra, cp_date, cprice
2008-07-07 05:40:00 1265.5 2008-07-07 1251.75
date_intra, time_intra, price_intra, cp_date, cprice
Out[13]:
(datetime.date(2008, 7, 7),
datetime.time(5, 40),
1265.5,
datetime.date(2008, 7, 7),
1251.75)
So from that, it all looks like I should be able to use: 这样看来,我应该可以使用:
datetime.datetime.combine(date_intra(), time_intra())
to combine the date object and the time object but i get the following error: 合并日期对象和时间对象,但出现以下错误:
TypeError: 'datetime.date' object is not callable
Because I am not great with Python I also tried: 因为我对Python不满意,所以我也尝试了:
datetime.datetime.combine(datetime.date(date_intra), datetime.time(time_intra))
But get the following error: 但是出现以下错误:
TypeError: an integer is required
Could anyone point out to me what i am doing wrong and perhaps correct me? 谁能指出我做错了什么,也许纠正了我?
Once I have combined those two (date_intra and time_intra) I wish to combine cp_date with time_intra. 一旦将这两个(date_intra和time_intra)结合起来,我希望将cp_date与time_intra结合起来。
You pass in the date and time objects directly : 您直接传递日期和时间对象:
datetime.datetime.combine(date_intra, time_intra)
These are already the correct type of objects that datetime.combine()
expects; 这些已经是
datetime.combine()
期望的正确对象类型。 there is no need to call them or to cast them to the correct type again . 没有必要给他们打电话或将它们重新转换为正确的类型。
Demo: 演示:
>>> import datetime
>>> date_intra = datetime.date(2008, 7, 7)
>>> time_intra = datetime.time(5, 40)
>>> datetime.datetime.combine(date_intra, time_intra)
datetime.datetime(2008, 7, 7, 5, 40)
You could just parse the time and date in one step : 您可以一步一步解析时间和日期:
date_intra, time_intra, price_intra, cp_date, cprice = (row[0], row[1], float(row[2]), row[3], float(row[4]))
intra = datetime.datetime.strptime(
'{} {}'.format(date_intra.partition(' ')[0], time_intra),
'%d/%m/%Y %H:%M:%S')
This takes the date portion of the date_intra
string (everything before the first space) and appends the time_intra
column text to that to parse out a combined date and time. 这将使用
date_intra
字符串的日期部分(第一个空格之前的所有内容),并将time_intra
列文本附加到该文本部分以解析出组合的日期和时间。
The offending line is 冒犯的行是
datetime.datetime.combine(date_intra(), time_intra())
This is where you're treating two variables, date_intra
and time_intra
as functions, and you're trying to call them. 在这里,您将两个变量
date_intra
和time_intra
当作函数,然后尝试调用它们。 Remove the parentheses: 删除括号:
>>> datetime.datetime.combine(date_intra, time_intra)
datetime.datetime(2008, 7, 7, 5, 40)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.