Python ISO encoding in UTF8

I am trying to read my letters using a Python script (Python 2.5 and PyPy). Some of my results are not in ASCII, and I get the following lines:

=? ISO-8859-7? AT? 0OXm7 / Dv8d / hIPP07 + 0gyuno4enx / u3h? = '

Is there a way to decode it and convert to utf-8 so that I can process it? I tried .decode ('ISO-8859-7') but I got the same line

+3
source share
2 answers
import email.header as eh

unicode_data= u''.join(
    str_data.decode(codec or 'ascii')
    for str_data, codec
    in eh.decode_header('=?ISO-8859-7?B?0OXm7/Dv8d/hIPP07+0gyuno4enx/u3h?='))
# unicode_data now is u'Πεζοπορία στον Κιθαιρώνα'

Here you should work with unicode_data. However, if you (it seems, you) need a UTF-8 encoded string, you can:

utf8data= unicode_data.encode('utf-8')

: .decode, , codec None (, eh.decode_header('plain text'))

+5

Source: https://habr.com/ru/post/1743025/


All Articles