我正在尝试使用calendar-view包构建一个日历图像。以下是来自calendar-view的代码
from calendar_view.calendar import Calendar
from calendar_view.core import data
from calendar_view.core.event import Event
config = data.CalendarConfig(
lang='en',
title='Sprint 23',
dates='2019-09-23 - 2019-09-27',
show_year=True,
mode='working_hours',
legend=False,
)
events = [
Event('Planning', day='2019-09-23', start='11:00', end='13:00'),
Event('Demo', day='2019-09-27', start='15:00', end='16:00'),
Event('Retrospective', day='2019-09-27', start='17:00', end='18:00'),
]
data.validate_config(config)
data.validate_events(events, config)
calendar = Calendar.build(config)
calendar.add_events(events)
calendar.save("sprint_23.png")这段代码运行得很好,然而,我一直在尝试动态地构建一个类似的日历。与中一样,事件的数量可能增加也可能减少。有没有办法让这段代码表现得更动态呢?
以下是我如何尝试将其动态化的一些信息:我将事件详细信息(如下所示)写入到txt文件中。
[
Event('Planning', day='2019-09-23', start='11:00', end='13:00'),
Event('Demo', day='2019-09-27', start='15:00', end='16:00'),
Event('Retrospective', day='2019-09-27', start='17:00', end='18:00'),
]然后将文本文件读入'event‘字段(如下所示)
event = open("instruction.txt", "r")但是代码失败了,因为它将从文件中读取的所有内容都视为“str”。请查看下面的错误:
AttributeError: 'str' object has no attribute 'get_start_date'发布于 2021-02-03 06:56:58
您可以使用以下代码:
with open('instruction.txt', 'r') as file:
file_content = file.read()
events = eval(file_content)eval()是执行字符串并返回Python对象的函数。
https://stackoverflow.com/questions/65997622
复制相似问题