将日期添加到列表中,但 datetime.datetime 不可迭代



我想得到房子被占用的日期对。这就是为什么我尝试将日期添加到列表中,但datetime.datetime不可迭代。

我在 booking.py 的文件中执行此操作:

import mysql.connector
from datetime import datetime, time
import dateparser
import pendulum
import string
import dateutil.parser
from robot.libraries.DateTime import convert_time
# function to return a tuple from the pendulum object type
def from_pendulum_to_tupple(date):
    year = date.year
    month = date.month
    day = date.day
    hour = date.hour
    minute = date.minute
    return (year, month, day, hour, minute)
# function to check if the room asked is free while looking in the database
def is_the_room_available(name_room, day_only, day_startinghour, day_ending_hour, cnx):
    # variables
    starting_hour_list = []
    ending_hour_list = []
    room_list = []
    #cursor
    cur_select_all = cnx.cursor(buffered=True)
    query_select_all = ("SELECT * FROM reservations")
    cur_select_all.execute(query_select_all)
    # convert the entry starting and ending meeting hour to a tupple
    asked_starting_hour = from_pendulum_to_tupple(day_startinghour)
    asked_ending_hour = from_pendulum_to_tupple(day_ending_hour)
    # select all the name room, starting and ending meeting hour and append them to a list
    for i in cur_select_all:
        room_list.append(i[1])
        starting_hour_list.append(from_pendulum_to_tupple(pendulum.parse(i[2])))
        ending_hour_list.append(from_pendulum_to_tupple(pendulum.parse(i[3])))
# ... Other stuff ...

打印返回:

cur_select_all:
CMySQLCursorBuffered: SELECT * FROM reservations
i[2]:
2018-08-08 12:00:00

但是,当您将它们添加到starting_hour_list时,会出现一个错误:

  File "C:UsersantoiDocumentsProgrammingNathalie18_2_2019starter-pack-rasa-stackbooking.py", line 42, in is_the_room_available
    starting_hour_list.append(from_pendulum_to_tupple(pendulum.parse(i[2])))
  File "C:UsersantoiDocumentsProgrammingNathalie18_2_2019starter-pack-rasa-stackstaenvlibsite-packagespendulumparser.py", line 20, in parse
    return _parse(text, **options)
  File "C:UsersantoiDocumentsProgrammingNathalie18_2_2019starter-pack-rasa-stackstaenvlibsite-packagespendulumparser.py", line 36, in _parse
    parsed = base_parse(text, **options)
  File "C:UsersantoiDocumentsProgrammingNathalie18_2_2019starter-pack-rasa-stackstaenvlibsite-packagespendulumparsing__init__.py", line 70, in parse
    return _normalize(_parse(text, **_options), **_options)
  File "C:UsersantoiDocumentsProgrammingNathalie18_2_2019starter-pack-rasa-stackstaenvlibsite-packagespendulumparsing__init__.py", line 111, in _parse
    return _parse_iso8601_interval(text)
  File "C:UsersantoiDocumentsProgrammingNathalie18_2_2019starter-pack-rasa-stackstaenvlibsite-packagespendulumparsing__init__.py", line 211, in _parse_iso8601_interval
    if "/" not in text:
TypeError: argument of type 'datetime.datetime' is not iterable

那么如何将日期时间添加到列表中呢?

要重现:

代码来自聊天机器人的项目。

从阅读pendulum文档来看,您似乎正在传递一个datetime对象,而不是pendulum.parse(str)需要的str。 您可以使用pendulum.instance(datetime object)代替parse(str)但由于它已经是一个datetime对象,因此您不需要这个额外的步骤(基于您实现from_pendulum_to_tuple的方式(。

# select all the name room, starting and ending meeting hour and append them to a list
print("cur_select_all: ")
print(cur_select_all)
# I wanted to unpack your tuple to make it easier to read
# but I do not know how big your tuple is so I added the *rest syntax
# which puts the rest of the elements in a new list called rest.
for x, room, start_time, end_time, *rest in cur_select_all:
    room_list.append(room)
    print("start_time: ", start_time)
    starting_hour_list.append(from_pendulum_to_tupple(start_time))
    ending_hour_list.append(from_pendulum_to_tupple(end_time))

最新更新