Convert Python to ISO-8859-5

I have encountered problems when trying to convert a UTF-8 file (containing Russian characters) to an ISO-8859-5 file: the "charmap" codec cannot encode the u '\ ufeff' character at position 0: character of the card. Did anyone understand what was wrong (?) Gave the following:

def convert():
    try:
        import codecs
        data = codecs.open('in.txt', 'r', 'utf-8').read()
    except Exception, e:
        print e
        sys.exit(1)

    f = open('out.txt', 'w')

    try:
        f.write(data.encode('iso-8859-5'))
    except Exception, e:
        print e
    finally:
        f.close()

"in.txt":! -№% "" (eupoyuyyaafyklzh;

+3
source share
2 answers

feff - Byte-Order-Mark symbol . ISO-8859-5 will have no idea.

You need to remove it from the variable databefore encoding it in ISO-8859-5.

+2
source

Python utf-8-sig, UTF-8 :

>>> print '\xef\xbb\xbf\xe3\x81\x82'.decode('utf-8-sig')
あ
+2

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


All Articles