이메일을 구문 분석하기위한 스크립트를 작성했습니다. Mac OS X Mail 클라이언트 (지금까지 테스트 한 것)에서 편지를 받았을 때 제대로 작동하지만 글자에 유니 코드 문자가 포함되어 있으면 파서가 실패합니다.본문에 유니 코드 문자가있을 때 파이썬으로 Gmail 이메일 구문 분석하기
예를 들어 내용이 ąčę
인 메시지를 보냈습니다.
그리고 여기에 동시에 몸과 첨부 파일을 구문 분석 스크립트의 내 부분이다 : 나는 OS X 메일에서 편지와 함께 작동 언급, 잘
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 = ''
하지만 Gmail을 문자로하지 않습니다 .
역 추적 :
Traceback (most recent call last): File "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/core/handlers/base.py", line 116, in get_response response = callback(request, *callback_args, **callback_kwargs) File "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 77, in wrapped_view return view_func(*args, **kwargs) File "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/views/decorators/http.py", line 41, in inner return func(request, *args, **kwargs) File "/Users/aemdy/PycharmProjects/rezervavau/bms/messages/views.py", line 66, in accept Message.accept(request.POST.get('msg')) File "/Users/aemdy/PycharmProjects/rezervavau/bms/messages/models.py", line 261, in accept thread=thread File "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/db/models/manager.py", line 149, in create return self.get_query_set().create(**kwargs) File "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/db/models/query.py", line 391, in create obj.save(force_insert=True, using=self.db) File "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/db/models/base.py", line 532, in save force_update=force_update, update_fields=update_fields) File "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/db/models/base.py", line 627, in 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", line 215, in _insert return insert_query(self.model, objs, fields, **kwargs) File "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/db/models/query.py", line 1633, in insert_query return 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", line 920, in execute_sql cursor.execute(sql, params) File "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/db/backends/util.py", line 47, in execute sql = self.db.ops.last_executed_query(self.cursor, sql, params) File "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/operations.py", line 201, in last_executed_query return cursor.query.decode('utf-8') File "/Users/aemdy/virtualenvs/django1.5/lib/python2.7/encodings/utf_8.py", line 16, in decode return codecs.utf_8_decode(input, errors, True) UnicodeDecodeError: 'utf8' codec can't decode byte 0xe0 in position 115: invalid continuation byte
내 스크립트가 나에게 다음과 같은 몸 ����
을 제공합니다. ąčę
을 다시 얻으려면 어떻게 해독 할 수 있습니까?
당신이 보내는 특수 문자가있는 문자열입니다 라틴어-1 인코딩하고 그것을 분명히 실패하는 utf-8로 해석하려고합니다. –
하지만 전자 메일 메시지의 본문을 구문 분석하는 보편적 인 방법이 필요합니다. 어떻게하면 될까요? 디코딩 latin-1 결과는'àèæë' – aemdy