Parsing files (ics/ icalendar) using Python

后端 未结 5 1490
感情败类
感情败类 2020-11-29 00:30

I have a .ics file in the following format. What is the best way to parse it? I need to retrieve the Summary, Description, and Time for each of the entries.

         


        
5条回答
  •  广开言路
    2020-11-29 00:36

    You could probably also use the vobject module for this: http://pypi.python.org/pypi/vobject

    If you have a sample.ics file you can read it's contents like, so:

    # read the data from the file
    data = open("sample.ics").read()
    
    # parse the top-level event with vobject
    cal = vobject.readOne(data)
    
    # Get Summary
    print 'Summary: ', cal.vevent.summary.valueRepr()
    # Get Description
    print 'Description: ', cal.vevent.description.valueRepr()
    
    # Get Time
    print 'Time (as a datetime object): ', cal.vevent.dtstart.value
    print 'Time (as a string): ', cal.vevent.dtstart.valueRepr()
    

提交回复
热议问题