我有一个.ics文件,我需要从
中删除VTIMEZONE行从BEGIN:VTIMEZONE到END:VTIMEZONE
删除所有内容的最简单方法是什么?BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//TEST//TEST//EN
BEGIN:VTIMEZONE
TZID:UTC+11
BEGIN:STANDARD
DTSTART;VALUE=DATE:20170101
TZNAME:UTC+11
TZOFFSETFROM:+1100
TZOFFSETTO:+1100
END:STANDARD
END:VTIMEZONE
BEGIN:VEVENT
DESCRIPTION:Testing
DTEND;TZID=UTC+11:20170228T233000
DTSTAMP:20170227T064302Z
DTSTART;TZID=UTC+11:20170228T230000
UID:1961094_636238800000000000
END:VEVENT
END:VCALENDAR
我这样做的原因是iCalendar库在包含vtimezone时没有解析文件。 DTSTART和DTEND不能是进程。
答案 0 :(得分:0)
我没有日历文件的经验,但是以下代码(提供注释以供解释):
with open("help.ics","r") as input_file:
#read in each line as a list
input_lines = input_file.readlines()
with open("newhelp.ics","w") as output_file:
#variable to determine whether to write file to new file or not
keep_lines = True
#loop thorugh every line
for line in input_lines:
if line == "BEGIN:VTIMEZONE\n":
#When the BEGIN:VTIMEZONE line is reached, stop writing lines
keep_lines = False
if keep_lines:
#write line to new file if the line is not inbetween the VTIMEZONE lines
output_file.write(line)
elif line == "END:VTIMEZONE\n":
#When the END:VTIMEZONE line is reached, start writing lines again
keep_lines = True
将输出一个新文件:
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//TEST//TEST//EN
BEGIN:VEVENT
DESCRIPTION:Testing
DTEND;TZID=UTC+11:20170228T233000
DTSTAMP:20170227T064302Z
DTSTART;TZID=UTC+11:20170228T230000
UID:1961094_636238800000000000
END:VEVENT
END:VCALENDAR
来自输入:
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//TEST//TEST//EN
BEGIN:VTIMEZONE
TZID:UTC+11
BEGIN:STANDARD
DTSTART;VALUE=DATE:20170101
TZNAME:UTC+11
TZOFFSETFROM:+1100
TZOFFSETTO:+1100
END:STANDARD
END:VTIMEZONE
BEGIN:VEVENT
DESCRIPTION:Testing
DTEND;TZID=UTC+11:20170228T233000
DTSTAMP:20170227T064302Z
DTSTART;TZID=UTC+11:20170228T230000
UID:1961094_636238800000000000
END:VEVENT
END:VCALENDAR
要保存要保留的行,您可以使用相同的方法,而不是打开新文件并在那里写行,您可以创建新列表lines_to_keep =[]
并执行lines_to_keep.append(line)
而不是output_file.write(line)
:
lines_to_keep = []
with open("help.ics","r") as input_file:
input_lines = input_file.readlines()
keep_lines = True
for line in input_lines:
if line == "BEGIN:VTIMEZONE\n":
keep_lines = False
if keep_lines:
#add the line to the list instead of writing it to a new file
lines_to_keep.append(line)
elif line == "END:VTIMEZONE\n":
keep_lines = True
然后lines_to_keep
列表包含您希望保持运行的所有行:
for line in lines_to_keep:
print(line)
输出:
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//TEST//TEST//EN
END:VTIMEZONE
BEGIN:VEVENT
DESCRIPTION:Testing
DTEND;TZID=UTC+11:20170228T233000
DTSTAMP:20170227T064302Z
DTSTART;TZID=UTC+11:20170228T230000
UID:1961094_636238800000000000
END:VEVENT
END:VCALENDAR
请注意,列表中的实际项目如下所示:
['BEGIN:VCALENDAR\n', 'VERSION:2.0\n', 'PRODID:-//TEST//TEST//EN\n', 'END:VTIMEZONE\n', 'BEGIN:VEVENT\n', 'DESCRIPTION:Testing\n', 'DTEND;TZID=UTC+11:20170228T233000\n', 'DTSTAMP:20170227T064302Z\n', 'DTSTART;TZID=UTC+11:20170228T230000\n', 'UID:1961094_636238800000000000\n', 'END:VEVENT\n', 'END:VCALENDAR']
\n
代表一个新行。没有它,如果你要将这些行写入新文件,它们将被写在同一行。 \n
首先出现在那里,因为它由input_file.readlines()
读入。
如果您想将这些转换为实际行的字符串,例如'BEGIN:VCALENDAR'
而不是'BEGIN:VCALENDAR\n'
,则可以遍历列表中的每一行,如果它以'结尾'结尾\ n'删除它:
for line_index in range(len(lines_to_keep)):
if lines_to_keep[line_index].endswith('\n'):
lines_to_keep[line_index] = lines_to_keep[line_index][:-1]
如果以'\ n'结尾,那么每行都会更改每行,而不是最后两个字符。
答案 1 :(得分:0)
感谢Adam的帮助,这是我的最终代码。我的微妙区别是我写入IO内存(import io)而不是文件。
ics_temp=io.StringIO()
with open(ics_local,"r") as input_file:
#read in each line as a list
input_lines = input_file.readlines()
ics_temp=io.StringIO()
keep_lines = True #variable to IOStream or not
for line in input_lines: #loop thorugh every line
if line == "BEGIN:VTIMEZONE\n":
#When the BEGIN:VTIMEZONE line is reached, stop writing lines
keep_lines = False
elif line == "END:VTIMEZONE\n":
#When the END:VTIMEZONE line is reached, start writing lines again
keep_lines = True
if keep_lines==True and line != "END:VTIMEZONE\n":
ics_temp.write(line) #write line to ioString inbetween the VTIMEZONE lines
ics_file=ics_temp.getvalue()
ics_temp.close()
print(ics_file)