Parsing files (ics/ icalendar) using Python -


I have .ics file in the following format, what is the best way to parse it? I need to retrieve the summary, details and time for each entry.

  BEGIN: Visilendar X-Lots-charge: UTF-8 version 2.0 Prodeed: - // Lotus Development Corporation // NONSGML Notes 8.0 / N Method: Publication Start: Vitiaimafon Tijiaidi: India Started Standard DTSTAR: 19000101 T 20000 TJFFSTFM: +0530 TJFFSTO: +0530Ed: Standard Inter VTIMZEN Start: VAITNT DTStart; TGID = "India": 20100615 T111500 dtnd; TZID = "India": 20100615T121500 TRANSP: Opaque DTSTAMP: 20100713T071035Z Category: Public Description: Email \ nDarlene \ n Murphy \ NDR. Ferri \ NUID: 12D3901F0AD9E83E65257743001F2C9A-Lotus_Notes_Generated X-lotus update-self-test question: 1 x-lotus update- WISL: $ S: 1; $ L: 1; $ B: 1; $ R: 1; $ E: 1; $ W: 1; $ He: 1; $ M: 1 NOTESVERSION of X-lotus: APPTTYPE of 2x-lotus: 0x-lotus CHILD_UID: 12D3901F0AD9E83E65257743001F2C9A End VEVENT: VEVENT DTSTART; TZID = "India": 20100628T130000 DTEND; TZID = "India": 20100628T133000 TRANSP: OPAQUE DTSTAMP: 20100628T055408Z class: public Description: Summary: Smart Energy Management Area: 8778/92050462 UID: 07 F-96 A3 F1 C 9,547,366,525,775,000,203th D 96-Lots_ Nots- X-Lots-updates -SEC: 1 x-lotus-upgraded- WISL: $ S: 1; $ L: 1; $ B: 1; $ R: 1; $ E: 1; $ W: 1; $ O: 1; $ M: 1 X-Lot-notes version: 2 X-Lotus-Notiteksai: X-Lots-Apititeepii: 3 X-Lots-Chaild_uaidi: 07 F-96 A3 F1 C 9,547,366,525,775,000,203th D 96 ED: Viavent start Viavent Ditistrt; TGID = "India": 20100629 T 110000 dtnd; Tijiaidi = "India": 20100629 T 120000 TRANSP: Opekyu Ditistimpi: 20,100,713 t 070 337 Class: Public Summary Meeting UID: 6011DDDD659E49D765257751001D2B4B-Lotus_Notes_Generated X-lotus update-SEQ: 1 X-lotus Update WISL: $ S: 1; $ L: 1; $ B: 1; $ R: 1; $ E: 1; $ W: 1; $ He: 1; $ M: 1 X-lotus NOTESVERSION: 2 X-lotus APPTTYPE: 0 X-lotus CHILD_UID: 6011DDDD659E49D765257751001D2B4B end: VEVENT  

Looks good.

For example, to write a file:

Import the calendar from the calendar, import it from the import of the day, import PITES from the time of import UTC # timezone Cal = calendar () cal.add ('prodid', '- // my calendar product // mxm.dk //') cal.add ('version', '2.0') event = event (event.add () 'Summary', 'Python meeting about calendaring') event.add ('dtstart', datetime (2005,4,4,8,0,0, tzinfo = UTC)) event.add ('dtend', datetime ( 2005,4,4,10,0,0, tzinfo = UTC)) Event.add ('dtstamp', datetime (2005,4,4,0,10), 0, tzinfo = UTC)) Event ['UID' ] = '20050115T101010 / 2 7346262376@mxm.dk 'event.add (' priority ', 5) cal.add_component (event) f = open (' example.ics', 'wb') f.write (cal.to_ical ()) f.close ( )

Tadaaa, you get this file:

  BEGIN: VCALENDAR PRODID: - // My calendar product // mxm.dk // VERSION: 2.0 starts: VEVENT DTEND; VALUE = date: 20050404T100000Z DTSTAMP; VALUE = Date: 20050404T001000Z DTSTART; VALUE = Date: 20050404T080000Z priority: 5 Summary: calendaring dragon meeting about UID: 20050115T101010/27346262376@mxm.dk end: Venture inter Visilendar  

But what is this file?

  g = open ('example.ics', 'rb') gcal = calendar.from_ical (g.read ()) for component in Gcal.walk (): print component.Name You can easily see:  
  & gt; & Gt; & Gt; VCALENDAR VEVENT & gt; & Gt; & Gt;  

About parsing data about events:

  g = open ('example.ics', 'rb') gcal = calendar. G.read () for component in to_ical (Gcal.walk ()): if component.name == "VEVENT": print component.get ('summary') print component.get ('dtstart') print component .get ('Dtend') Print component.gate ('dtstamp') g.close ()  

Now you get:

  & gt; & Gt; & Gt; 20050404T080000Z 20050404T100000Z 20050404T001000Z> About Calendaring & Gt; Python About Calendar  

Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -