带有ICS文件和CSV的Python [英] Python with ICS files and CSV

查看:67
本文介绍了带有ICS文件和CSV的Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个朋友问我一个个人项目的帮助,但是我不得不承认我的Python技能是非常基础的.

one friend ask me some help on a personnal project, but I have to admit my skills in Python are very basics.

这里是个主意:

我必须下载Multplie ics文件(Google日历文件).我找到了multidl python程序.它完美地工作.我必须下载的所有文件URL都存储在一个txt文件中.

I have to download multplie ics files (google calendar files). I found multidl python program. It works perfectly. All files url that I have to download are store in a txt file.

文件格式类似于YYYYMMDD_Merdy.ics YYYYMMDD_test.ics

files format is like YYYYMMDD_Merdy.ics YYYYMMDD_test.ics

此处是ics文件的一个示例

here an exemple of ics files

BEGIN:VCALENDAR
PRODID:-//Google Inc//Google Calendar 70.9054//EN
VERSION:2.0
CALSCALE:GREGORIAN
METHOD:PUBLISH
X-WR-CALNAME:Chambre Champêtre
X-WR-TIMEZONE:Europe/Paris
BEGIN:VEVENT
DTSTART:20180407T140000Z
DTEND:20180408T080000Z
DTSTAMP:20181002T185454Z
UID:3a4j71mpemgjoo66anfd0tcvrh@google.com
CREATED:20180401T165816Z
DESCRIPTION:
LAST-MODIFIED:20180401T165816Z
LOCATION:
SEQUENCE:0
STATUS:CONFIRMED
SUMMARY:direct Chantal
TRANSP:OPAQUE
END:VEVENT
BEGIN:VEVENT
DTSTART:20181001T150000Z
DTEND:20181002T080000Z
DTSTAMP:20181002T185454Z
UID:ccs6aor5cor3cbb270r3ab9kcpi6abb26di34b9kc9im8chj75hm4d35cc@google.com
CREATED:20181001T154801Z
DESCRIPTION:
LAST-MODIFIED:20181001T154801Z
LOCATION:
SEQUENCE:0
STATUS:CONFIRMED
SUMMARY:Ferme
TRANSP:OPAQUE
END:VEVENT
END:VCALENDAR

我想要的输出是具有followinf格式的csv文件:

The output I want is a csv file with the followinf format:

X-WR-CALNAME;DTSTART;DTEND;CREATED;LOCATION;SUMMARY
Chambre Champêtre;20180407T140000Z;20180408T080000Z;20180401T165816Z;direct Chantal;
Chambre Champêtre;20181001T150000Z;20181002T080000Z;20181001T154801Z;Ferme

输出的csv文件应包含ICS文件中的所有转换

The output csv file should contain all conversion from ICS files

随意问我

谢谢大家的帮助.

推荐答案

下面是一个解决方案.让我知道是否有帮助.在这里,我从目录C:/Users/Dell/Documents中读取所有.ics文件.您的情况有所不同.

One solution for this is below. Let me know if it helps. Here I am reading all .ics file from directory C:/Users/Dell/Documents. In your case it is different.

from os import listdir

filenames = listdir(r'C:/Users/Dell/Documents')

for filename in filenames:
    if filename.endswith('.ics'):
        file=open(filename,"r")
        outputhead=['X-WR-CALNAME','DTSTART','DTEND','CREATED','LOCATION','SUMMARY']
        datalist=[]
        outfile=open(filename+'ouput.csv',"w")
        outfile.write(';'.join(outputhead))
        outfile.write('\n')
        outfile.close()
        outfile=open(filename+'ouput.csv',"a+")
        for i in file:
            if i.split(':')[0] in outputhead:
                datalist.append(i.split(':')[1].replace('\n',''))
            if i.split(':')[0]=='SUMMARY':
                outfile.write(';'.join(datalist))
                outfile.write('\n')
                datalist=[]
        outfile.close()

这篇关于带有ICS文件和CSV的Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆