【发布时间】:2019-04-17 14:53:58
【问题描述】:
我正在做a chatbot to book rooms。我创建了一个函数来检查在数据库中查找时询问的房间是否空闲。在某些时候,我尝试将条目开始和结束会议时间转换为带有from_pendulum_to_tupple(day_startinghour) 和day_startinghour 的元组,例如2019-04-18T14:00:00+00:00
def from_pendulum_to_tupple(date):
print("date: ")
print(date)
print("type : " + str(type(date)))
year = date.year
month = date.month
day = date.day
hour = date.hour
minute = date.minute
return (year, month, day, hour, minute)
但我有一个 AttributeError:str 对象没有属性 year。确实,错误信息是:
文件 "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\actions.py", 第 43 行,运行中 booking_answer = make_a_booking(name_room, day, hour_start, duration) 文件“C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\booking.py”, 第 94 行,在 make_a_booking 中 room_available = is_the_room_available(name_room, day_only, pendulum_combined_day_and_hour_start, pendulum_combined_day_and_hour_end, cnx) 文件“C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\booking.py”, 第 52 行,在 is_the_room_available start_hour_list.append(from_pendulum_to_tuple(start_time)) 文件“C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\booking.py”, 第 14 行,在 from_pendulum_to_tuple 年 = 日期.年 AttributeError:“str”对象没有属性“year” 127.0.0.1 - - [2019-04-17 16:42:01] "POST /webhook HTTP/1.1" 500 412 1.050171
day_startinghour 是用make_a_booking 创建的,它需要空间,在调用上述函数前一天零一个小时,以了解房间是否在我们想要预订的时间使用:
def make_a_booking(name_room, day, hour_start, duration):
print(name_room, day, hour_start, duration)
# connect to the localhost database
cnx = mysql.connector.connect(password='MySQL.2019', user="root", database="alex")
#day_only : get the parsed date
day_only = str(dateparser.parse(day).date())
# parse the hour in string inputed by the user and convert it the a pendulum object
hour_start_parsed = dateutil.parser.parse(hour_start, fuzzy_with_tokens=True)
pendulum_combined_day_and_hour_start = pendulum.parse(str(day_only) + " " + hour_start, strict=False)
# convert the duration in string inputed by the user and to seconds then in minutes
duration_in_seconds = convert_time(duration)
duration_in_minutes = duration_in_seconds / 60
# add the duration_in_minutes to the starting hour to get the hour start pendulum object
pendulum_combined_day_and_hour_end = pendulum_combined_day_and_hour_start.add(minutes = duration_in_minutes)
#print(pendulum_combined_day_and_hour_end)
# check if the room is available
room_available = is_the_room_available(name_room, day_only, pendulum_combined_day_and_hour_start, pendulum_combined_day_and_hour_end, cnx)
【问题讨论】:
-
你能提供一个最小的例子吗?您是否打算将字符串日期转换为日期对象?如果是这样,也许可以为我们提供一个字符串日期的示例列表?
-
它的
str。只需使用datetime.strptime(...)将其转换为日期即可。 -
print("type : " + str(type(date)))你到底在做什么? -
print(type(date))你得到了什么? -
与您的问题无关,但您确实不应该在源代码文件中存储数据库的密码等,然后将它们推送到 github...
标签: python python-3.x date