aem*_*mdy 2 python email unicode
我写了一个脚本来解析一封电子邮件.从Mac OS X Mail客户端接收信件时它工作正常(到目前为止只测试了这个),但是当字母在其正文部分中包含unicode字母时,我的解析器会失效.
例如,我发送了一条包含内容的消息???.
这是我的脚本部分同时解析正文和附件:
p = FeedParser()
p.feed(msg)
msg = p.close()
attachments = []
body = None
for part in msg.walk():
if part.get_content_type().startswith('multipart/'):
continue
try:
filename = part.get_filename()
except:
# unicode letters in filename, set default name then
filename = 'Mail attachment'
if part.get_content_type() == "text/plain" and not body:
body = part.get_payload(decode=True)
elif filename is not None:
content_type = part.get_content_type()
attachments.append(ContentFile(part.get_payload(decode=True), filename))
if body is None:
body = ''
Run Code Online (Sandbox Code Playgroud)
好吧,我提到它适用于来自OS X Mail的信件,但是使用Gmail信件却没有.
追溯:
回溯(最近一次调用最后一次):文件"/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/core/handlers/base.py",第116行,在get_response response = callback (request,*callback_args,**callback_kwargs)文件"/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/views/decorators/csrf.py",第77行,在wrapped_view中返回view_func(*args,**kwargs)文件"/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/views/decorators/http.py",第41行,内部返回函数(request,*args,**kwargs)文件"/Users/aemdy/PycharmProjects/rezervavau/bms/messages/views.py",第66行,接受Message.accept(request.POST.get('msg'))文件"/Users/aemdy/PycharmProjects/rezervavau/bms/messages/models.py",第261行,in accept thread = thread文件"/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/db/models/manager.py",第149行,在create return self.get_query_set() .create(**kwargs)文件"/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/db/models/query.py",第391行,在create obj.save中( force_insert = True,using = self.db)文件"/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/db/models/base.py",第532行,保存force_update = force_update,update_fields = update_fields)文件"/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/db/models/base.py",第627行,在save_base result = manager中. _insert([self],fields = fields,return_id = update_pk,using = using,raw = raw)File"/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/db/models/manager.py",第215行,在_insert中返回insert_query(self.model,objs,fields,**kwargs)文件"/Users/aemdy/virtualenvs/django1.5/ lib/python2.7/site-packages/django/db/models/query.py",第1633行,在insert_query中返回query.get_compiler(using = using).execute_sql(return_id)File"/ Users/aemdy/virtualenvs/django1 .5/lib/python2.7/site-packages/django/db/models/sql/compiler.py",第920行,在execute_sql cursor.execute(sql,params)文件"/ Users/aemdy/virtualenvs/django1. 5/lib/python2.7/site-packages/django/db/backends/util.py",第47行,执行sql = self.db.ops.last_executed_query(self.cursor,sql,params)文件"/ Users /aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/operations.py",第201行,在last_executed_query中返回cursor.query.decode('utf-8')文件"/Users/aemdy/virtualenvs/django1.5/lib/python2.7/encodings/utf_8.py",第16行,在解码返回codecs.utf_8_decode (input,errors,True)UnicodeDecodeError:'utf8'编解码器无法解码位置115的字节0xe0:无效的连续字节
我的脚本给了我以下的身体????.如何解码才能???回来?
好吧,我自己找到了解决方案.我现在会做一些测试,如果有任何失败,现在就让你们.
我需要再次解码身体:
body = part.get_payload(decode=True).decode(part.get_content_charset())
Run Code Online (Sandbox Code Playgroud)