pythondatetimeicalendardecoding

Parsing problems of vDDDTypes data with icalendar in python


as you may understand from the following I'm not a very skilled programmer. Nevertheless I'm trying to write a python program for importing and storing in a database data from an icalendar formatted file. The file has several occurrences like the following one (skipping the non relevant info):

BEGIN:VEVENT
UID:TU1586072026
DTSTAMP:20240125T161430Z
SUMMARY:My meeting
DESCRIPTION:None
...
CREATED:20231004T161313Z
LAST-MODIFIED:20231023T162939Z
END:VEVENT

My problem is in 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))

I get an error after it prints the first two in a proper 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'

While, if I don't use the .dt attribute in 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 get no 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({}))

looking exactly like I should use the .dt attribute as for the others.

What am I doing wrong?


Solution

  • Updated: This example works on my python environment, now with 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, if e.g. last-modified 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