Python 向列表中添加日期,但datetime.datetime不可编辑
我想得到一对房子有人住的日期。这就是为什么我试图将日期添加到列表中,但datetime.datetime不可编辑 我在文件中这样做: 打印返回:Python 向列表中添加日期,但datetime.datetime不可编辑,python,python-3.x,Python,Python 3.x,我想得到一对房子有人住的日期。这就是为什么我试图将日期添加到列表中,但datetime.datetime不可编辑 我在文件中这样做: 打印返回: cur_select_all: CMySQLCursorBuffered: SELECT * FROM reservations i[2]: 2018-08-08 12:00:00 但是当您将它们添加到开始时间列表中时出现错误: File "C:\Users\antoi\Documents\Programming\Nathalie\18_2_20
cur_select_all:
CMySQLCursorBuffered: SELECT * FROM reservations
i[2]:
2018-08-08 12:00:00
但是当您将它们添加到开始时间列表中时
出现错误:
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
那么,如何将日期时间添加到列表中呢
复制:
代码来自阅读
钟摆文档,看起来您传递的是日期时间
对象,而不是钟摆解析(str)
需要的str
。您可以使用datetime.instance(datetime对象)
而不是parse(str)
,但由于它已经是datetime
对象,您不需要这个额外的步骤(基于您如何实现从\u be摆\u到\u元组的过程)
通过阅读be摆
文档,您似乎正在传递一个datetime
对象,而不是str
所需的be摆.parse(str)
对象。您可以使用datetime.instance(datetime对象)
而不是parse(str)
,但由于它已经是datetime
对象,您不需要这个额外的步骤(基于您如何实现从\u be摆\u到\u元组的过程)
提供从_be摆_到_tuple
的源代码,如下所示well@DeveshKumarSingh是的,就这么做了。它位于链接中。请提供从_be摆_到_tuple
的源代码,如下所示well@DeveshKumarSingh是的,就这么做了。它在链接中。谢谢你的回答。然而,如果room\u list
是一个以开头的列表,对于x,room\u list,start\u time,end\u times,*rest in cur\u select\u all
现在是一个字符串。因此它创建了一个属性错误:“str”对象没有属性“append”
@ThePassenger Typo。修正了。谢谢你的回答。然而,如果room\u list
是一个以开头的列表,对于x,room\u list,start\u time,end\u times,*rest in cur\u select\u all
现在是一个字符串。因此它创建了一个属性错误:“str”对象没有属性“append”
@ThePassenger Typo。固定的。
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
# 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))