Parsing files (ics/ icalendar) using Python

后端 未结 5 1487
感情败类
感情败类 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:44

    New to python; the above comments were very helpful so wanted to post a more complete sample.

    # ics to csv example
    # dependency: https://pypi.org/project/vobject/
    
    import vobject
    import csv
    
    with open('sample.csv', mode='w') as csv_out:
        csv_writer = csv.writer(csv_out, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
        csv_writer.writerow(['WHAT', 'WHO', 'FROM', 'TO', 'DESCRIPTION'])
    
        # read the data from the file
        data = open("sample.ics").read()
    
        # iterate through the contents
        for cal in vobject.readComponents(data):
            for component in cal.components():
                if component.name == "VEVENT":
                    # write to csv
                    csv_writer.writerow([component.summary.valueRepr(),component.attendee.valueRepr(),component.dtstart.valueRepr(),component.dtend.valueRepr(),component.description.valueRepr()])
    
    

提交回复
热议问题