Home > Article > Backend Development > Problem using icalendar to parse vDDDTypes data in python
As you can probably gather from the following, I am not a very skilled programmer. Nonetheless, I'm trying to write a python program for importing data from a icalendar format file and storing it in a database. The file appears multiple times as shown below (skipping irrelevant information):
begin:vevent uid:tu1586072026 dtstamp:20240125t161430z summary:my meeting description:none ... created:20231004t161313z last-modified:20231023t162939z end:vevent
My problem lies with the decoding of the last-modified value.
If I run:
print("dtstamp: " + str(component.get('dtstamp').dt)) print("created: " + str(component.get('created').dt)) print("modified: " + str(component.get('last-modified').dt))
Error after printing the first two in the correct way:
dtstamp: 2024-01-25 16:14:30+00:00 created: 2023-10-04 16:13:13+00:00 traceback (most recent call last): file "/usr/lib/python3.11/tkinter/__init__.py", line 1948, in __call__ return self.func(*args) ^^^^^^^^^^^^^^^^ file "/home/sailslack/coding/python/pim/cal_import.py", line 97, in ical_import print("modified: " + str(component.get('last-modified').dt)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attributeerror: 'nonetype' object has no attribute 'dt'
If I don't use the .dt attribute on the last line:
print("dtstamp: " + str(component.get('dtstamp').dt)) print("created: " + str(component.get('created').dt)) print("modified: " + str(component.get('last-modified')))
I don't get any errors, but:
dtstamp: 2024-01-25 16:14:30+00:00 created: 2023-10-04 16:13:13+00:00 modified: vDDDTypes(2023-10-23 16:29:39+00:00, Parameters({}))
Looks like I should be using the .dt property like the other properties.
What did i do wrong?
Update: This example works in my python environment and now uses a try block to handle missing components:
from icalendar import calendar from datetime import datetime with open('icalendar.ics', 'rb') as e: ecal = calendar.from_ical(e.read()) for component in ecal.walk(): if component.name == 'vevent': print(component.name) com_attr = ['created','dtstamp','last-modified'] for timing in com_attr: try: print(f"{timing}: {component.get(timing).dt}") except attributeerror: print(f"{timing} -> does not exist!") com_text = ['uid','summary','description'] for tex in com_text: try: print(f"{tex}: {component.get(tex)}") except attributeerror: print(f"{tex} -> does not exist!")
Output, e.g. last modified time is missing:
VEVENT CREATED: 2023-10-04 16:13:13+00:00 DTSTAMP: 2024-01-25 16:14:30+00:00 LAST-MODIFIED -> does not exist! UID: TU1586072026 SUMMARY: My meeting DESCRIPTION: None
The above is the detailed content of Problem using icalendar to parse vDDDTypes data in python. For more information, please follow other related articles on the PHP Chinese website!