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

标签 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.

最佳答案

通过阅读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))

关于python - 将日期添加到列表中,但 datetime.datetime 不可迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55619420/

相关文章:

python - 如何在python中进行PCA和SVM分类

python - 是否可以使用时间戳检查两个不同日期之间的日期和时间戳?

python - 为什么在表达式字段中不允许生成器中未加括号的元组?

python - 如何替换 Pandas 数据框中字符串中的子字符串

python - 将数字数组格式化为货币,值之间具有最小宽度

python - 我们如何使用合并操作后找到的数据更新相似列的行中的列数据?

python - 为什么多重处理没有使用我所有的核心

python - 反转大型 JSON 字典

Python导入模块依赖

python - 如何将不规则时间序列重新采样为每日频率并将其跨越到今天?