0

我在谷歌有一个日历来保存我的日志。如果我导出它,它看起来像 addendum1。目的是将此文本编译为干净的输出。

我需要的是一个仅包含事件日期和描述的列表。因此我开始使用这段代码来测试它(我是 python 100% 的新手)。

*#!/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')]*

它做了我想要的,但现在我需要去寻找另外两个事件。而且我不知道如何将 find 选项用于第二个,第三个... find 选项?

如果这可行,那么我可以在一个while循环中覆盖它,最多可以覆盖amount_events。我的最终目标是学习如何使用 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
4

1 回答 1

0

要将 .ics 文件转换为文本,您需要导入以下 jars

    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;
      }

     }

    }
于 2020-04-24T18:48:27.347 回答