我正在尝试运行一个脚本(我找到here)来读取.ics文件(我从谷歌日历中获取)。这是代码:
from icalendar import Calendar
import urllib
ics = urllib.urlopen('http://www.google.com/calendarlink/basic.ics').read()
ical=Calendar.from_ical(ics)
for vevent in ical.subcomponents:
if vevent.name != "VEVENT":
continue
title = str(vevent.get('SUMMARY'))
description = str(vevent.get('DESCRIPTION'))
location = str(vevent.get('LOCATION'))
start = vevent.get('DTSTART').dt # a datetime
end = vevent.get('DTEND').dt # a datetime
我收到此错误:
C:\python test>c:\Python27\python.exe google-calendar-test.py
Traceback (most recent call last):
File "google-calendar-test.py", line 8, in <module>
title = str(vevent.get('SUMMARY'))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 47:
ordinal not in range(128)
任何人都知道我做错了什么?
编辑: 答案:我不得不将第8行更改为:
title = str(vevent.get('SUMMARY').encode('utf-8'))