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

时间:2019-04-10 18:46:46

标签: python python-3.x

我想获取一所房子被占用的日期。这就是为什么我试图将日期添加到列表中,但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:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\booking.py", line 42, in is_the_room_available
    starting_hour_list.append(from_pendulum_to_tupple(pendulum.parse(i[2])))
  File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\staenv\lib\site-packages\pendulum\parser.py", line 20, in parse
    return _parse(text, **options)
  File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\staenv\lib\site-packages\pendulum\parser.py", line 36, in _parse
    parsed = base_parse(text, **options)
  File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\staenv\lib\site-packages\pendulum\parsing\__init__.py", line 70, in parse
    return _normalize(_parse(text, **_options), **_options)
  File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\staenv\lib\site-packages\pendulum\parsing\__init__.py", line 111, in _parse
    return _parse_iso8601_interval(text)
  File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_2019\starter-pack-rasa-stack\staenv\lib\site-packages\pendulum\parsing\__init__.py", line 211, in _parse_iso8601_interval
    if "/" not in text:
TypeError: argument of type 'datetime.datetime' is not iterable

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

要复制:

代码来自a project for a chatbot.

1 个答案:

答案 0 :(得分:0)

通过阅读pendulum文档,您似乎正在传递datetime对象,而不是str需要的pendulum.parse(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))