vcard = put.get('vcard')
try:
vcard_readable = base64.decodestring(vcard)
quoted_printable_vcard = quopri.encodestring(vcard_readable)
vobj = vobject.readOne(quoted_printable_vcard)
except UnicodeEncodeError as e: # case of bad encoding
error_message = {
"valid": False,
"reason": "Invalid vCard\n{0}".format(e)}
return HttpResponse(json.dumps(error_message), status=200)
except vobject.base.VObjectError as e2: # case of invalid vcard
error_message = {
"valid": False,
"reason": "Invalid vCard format\n{0}".format(e2)}
return HttpResponse(json.dumps(error_message), status=200)
except:
error_message = {
"valid": False,
"reason": "Invalid vCard."}
return HttpResponse(json.dumps(error_message), status=200)
好的。我解决了。要vobject
使用 UNICODE (UTF-8),需要使用:
quoted printable encoding.
- 以下示例:
>>> s = "gżegżółka"
>>> s
'g\xc5\xbceg\xc5\xbc\xc3\xb3\xc5\x82ka'
>>> import quopri
>>> quopri.encodestring(s)
'g=C5=BCeg=C5=BC=C3=B3=C5=82ka'