我想将Google日历的ics文件转换为仅包含日期和描述的文本文件

0 投票
1 回答
1360 浏览
提问于 2025-04-17 18:16

我在谷歌日历上有一个日记,用来记录我的日常活动。如果我把它导出,结果就像附录1那样。我的目的是把这些文字整理成一个干净的输出。

我需要的是一个只包含事件日期和描述的列表。为此,我开始写了一段代码来测试一下(我对Python完全是新手)。

*#!/usr/bin/env python
basic=open('/Users/geertesselens/Desktop/Temp/basic.txt','r')
gcal=basic.read()
basic.close()
amount_events = gcal.count('BEGIN:VEVENT')
print
print gcal[gcal.find('DTSTART')+19:gcal.find('DTSTART')+27],
print "   ",
print gcal[gcal.find('DESCRIPTION')+12:gcal.find('LAST-MODIFIED')]*

这段代码达到了我想要的效果,但现在我需要去寻找另外两个事件。我不知道如何使用查找功能来找到第二个、第三个……事件。

如果能做到这一点,我就可以用一个循环来处理所有的事件数量。我的最终目标是学习如何使用Python作为一个快速的文本整理工具,以解决各种文本不匹配的问题。

任何反馈都是欢迎的。我会确保把最终结果反馈给你。

************附录1 - ICS文件***************

BEGIN:VCALENDAR
PRODID:-//Google Inc//Google Calendar 70.9054//EN
VERSION:2.0
CALSCALE:GREGORIAN
METHOD:PUBLISH
X-WR-CALNAME:DayLog
X-WR-TIMEZONE:Europe/Brussels
X-WR-CALDESC:
**BEGIN:VEVENT**
**DTSTART**;VALUE=DATE:20130308
DTEND;VALUE=DATE:20130309
DTSTAMP:20130307T143316Z
UID:ab2hdo4i6f6t0gsre81qb4m0o4@google.com
CREATED:20130306T093219Z
**DESCRIPTION**:comment1.
LAST-MODIFIED:20130306T093219Z
LOCATION:
SEQUENCE:0
STATUS:CONFIRMED
SUMMARY:8_3_13
TRANSP:TRANSPARENT
END:VEVENT
**BEGIN:VEVENT**
**DTSTART**;VALUE=DATE:20130307
DTEND;VALUE=DATE:20130308
DTSTAMP:20130307T143316Z
UID:3j01a76v6lvg5870obsrl5t29g@google.com
CREATED:20130306T093100Z
**DESCRIPTION**:comment2
LAST-MODIFIED:20130306T093100Z
LOCATION:
SEQUENCE:0
STATUS:CONFIRMED
SUMMARY:7_3_13
TRANSP:TRANSPARENT
END:VEVENT
BEGIN:VEVENT
DTSTART;VALUE=DATE:20130306
DTEND;VALUE=DATE:20130307
DTSTAMP:20130307T143316Z
UID:bs8tb662lpvt7en3h78t67rluo@google.com
ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=ACCEPTED;CN=DayLog
 ;X-NUM-GUESTS=0:mailto:kpekivj6fliclchr39rccpmtkk@group.calendar.google.com
CREATED:20130306T092953Z
DESCRIPTION:comment3a
 comment3b
 comment3c
LAST-MODIFIED:20130306T093034Z
LOCATION:
SEQUENCE:0
STATUS:CONFIRMED
SUMMARY:6_3_13
TRANSP:TRANSPARENT
END:VEVENT
END:VCALENDAR

1 个回答

0

要把 .ics 文件转换成文本,你需要导入下面的 jar 包。

    backport-util-concurrent-3.1.jar
    commons-lang-2.6-130003.jar
    commons-logging-1.1.1-1.0.0.jar
    ical4j-1.0.5.jar

把 ical4j.properties 文件放到你的源代码文件夹里。

    keep below properties with same sequence in 
    ical4j.properties file 

    ical4j.unfolding.relaxed=true
    ical4j.parsing.relaxed=true
    ical4j.validation.relaxed=true
    ical4j.compatibility.outlook=true
    ical4j.compatibility.notes=true 

使用下面的程序:

    import java.io.File;
    import java.io.FileInputStream;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    import net.fortuna.ical4j.data.CalendarBuilder;
    import net.fortuna.ical4j.model.Component;
    import net.fortuna.ical4j.model.Property;
    import net.fortuna.ical4j.model.property.CalScale;
    import net.fortuna.ical4j.model.property.ProdId;
    import net.fortuna.ical4j.model.property.Version;

    public class ParseIcsFileToText {

     public static void main(String args[]) throws Exception {
      ParseIcsFileToText ics = new ParseIcsFileToText();
      File file = new File("C://Users//Desktop//Swati.ics");
      String result = ics.getCalInfoFromIcs(file);
      System.out.println(result);
     }

     public String getCalInfoFromIcs(File file) throws Exception {
         CalendarBuilder builder = new CalendarBuilder();
      net.fortuna.ical4j.model.Calendar calendar = new net.fortuna.ical4j.model.Calendar();


      calendar.getProperties().add(new ProdId("-//Ben Fortuna//iCal4j 1.0//EN"));
      calendar.getProperties().add(Version.VERSION_2_0);
      calendar.getProperties().add(CalScale.GREGORIAN);


      FileInputStream fin = new FileInputStream(file);

      calendar = builder.build(fin);
      List result = new ArrayList<String>();

      for (Iterator i = calendar.getComponents().iterator(); i.hasNext();) {
       Map<String, String> calenderinfo = new HashMap<String, String>();
       Component component = (Component) i.next();

       for (Iterator j = component.getProperties().iterator(); j.hasNext();) {
        try {
         String startdate = null, enddate = null, event = null;
         Property property = (Property) j.next();
         if ("DTSTART".equals(property.getName())) {
          startdate = property.getValue();
          calenderinfo.put("startDate",
            modifyDateLayoutOfIcs(startdate));
         }
         if ("DTEND".equals(property.getName())) {
          enddate = property.getValue();
          calenderinfo.put("endDate",
            modifyDateLayoutOfIcs(enddate));
         }
         if ("SUMMARY".equals(property.getName())) {
          event = property.getValue();
          calenderinfo.put("event", event);
         }

         if (!calenderinfo.isEmpty()) {
          if (calenderinfo.get("event") != null) {
           result.add(calenderinfo);
          }
         }

        } catch (Exception ex) {

        }
       }
      }
      return result.toString();
     }

     private String modifyDateLayoutOfIcs(String inputDate) {
      try {

       SimpleDateFormat inDateFormat = new SimpleDateFormat("yyyyMMdd");
       java.util.Date fromDate = inDateFormat.parse(inputDate);

       SimpleDateFormat outDateForm = new SimpleDateFormat("dd-MMM-yyyy");
       return outDateForm.format(fromDate);
      } catch (Exception e) {
       return inputDate;
      }

     }

    }

撰写回答