Sunday, 15 February 2015

Printing email headers into a file in python -



Printing email headers into a file in python -

i have file each line containing subject of e-mail, encoded in utf-8 or big-5. decode these subjects in python , print them file. using python's email.header.decode_header. next code, prints file doesn't work:

from email.header import decode_header f = open("subjects.txt", 'r') g = open("transformed_subjects", 'w') line in f: dh = decode_header(line) print >> g, ' '.join(s.decode(enc or 'ascii') s,enc in dh ) f.close() g.close()

and next error:

traceback (most recent phone call last): file "read_subjects.py", line 8, in <module> print >> g, ' '.join(s.decode(enc or 'ascii') s,enc in dh ) unicodeencodeerror: 'ascii' codec can't encode characters in position 68-74: ordinal not in range(128)

but when print stdout, works:

from email.header import decode_header f = open("subjects.txt", 'r') line in f: dh = decode_header(line) print ' '.join(s.decode(enc or 'ascii') s,enc in dh ) f.close()

what difference?

python email

No comments:

Post a Comment