Следующий код работал хорошо, пока не обнаружил событие без описания поля (не уверен, как это произошло), есть ли способ перейти к следующему событию, если в одном событии обнаружены ошибки?
# ics to csv example
# dependency: https://pypi.org/project/vobject/
import vobject
import csv
with open('sample.csv', mode='w') as csv_out:
csv_writer = csv.writer(csv_out, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
csv_writer.writerow(['WHAT', 'WHO', 'FROM', 'TO', 'DESCRIPTION'])
# read the data from the file
data = open("sample.ics").read()
# iterate through the contents
for cal in vobject.readComponents(data):
for component in cal.components():
if component.name == "VEVENT":
# write to csv
csv_writer.writerow([component.summary.valueRepr(),component.attendee.valueRepr(),component.dtstart.valueRepr(),component.dtend.valueRepr(),component.description.valueRepr()])
Это работает как задумано сейчас. Спасибо @ stovfl
import vobject
import csv
with open('calendar2022.csv', mode='w') as csv_out:
csv_writer = csv.writer(csv_out, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
csv_writer.writerow(['WHAT', 'FROM', 'TO', 'DESCRIPTION', 'LOCATION'])
# read the data from the file
data = open("calendar.ics").read()
# iterate through the contents
for cal in vobject.readComponents(data):
for component in cal.components():
if component.name == "VEVENT":
writerow = []
for attr in ['summary', 'dtstart', 'dtend', 'description', 'location']:
if hasattr(component, attr):
writerow.append(getattr(component, attr).valueRepr())
else:
writerow.append('Undefined!')
print(writerow)
csv_writer.writerow(writerow)