次のように、ファイルからメールを読み取ろうとします。
import email
with open("xxx.eml") as f:
msg = email.message_from_file(f)
そして、私はこのエラーを受け取ります:
Traceback (most recent call last):
File "I:\fakt\real\maildecode.py", line 53, in <module>
main()
File "I:\fakt\real\maildecode.py", line 50, in main
decode_file(infile, outfile)
File "I:\fakt\real\maildecode.py", line 30, in decode_file
msg = email.message_from_file(f) #, policy=mypol
File "C:\Python33\lib\email\__init__.py", line 56, in message_from_file
return Parser(*args, **kws).parse(fp)
File "C:\Python33\lib\email\parser.py", line 55, in parse
data = fp.read(8192)
File "C:\Python33\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 1920: character maps to <undefined>
ファイルにはマルチパート メールが含まれており、その部分は UTF-8 でエンコードされています。ファイルの内容またはエンコーディングが壊れている可能性がありますが、とにかく処理する必要があります。
Unicode エラーがある場合でも、ファイルを読み取るにはどうすればよいですか? ポリシー オブジェクトが見つかりませんcompat32
。例外を処理して、例外が発生した場所で Python を続行させる方法がないようです。
私に何ができる?