0

I want write some strings to file which is not in English, they are in Azeri language. Even if I do utf-8 encoding I get following error:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 10-12: ordinal not in range(128)

my code piece that wants to write to file is following:

        t_w = text_list[y].encode('utf-8')
        new_file.write(t_w.decode('utf-8'))
        new_file.write('\n')

EDIT

Even if I make code as:

        t_w = text_list[y].encode('ascii',errors='ignore')
        new_file.write(t_w)
        new_file.write('\n')

I get following error which is :

TypeError: write() argument must be str, not bytes

1 Answers1

0

From what I can tell t_w.decode(...) attempts to convert your characters to ASCII, which doesn't encode some Azeri characters. There is no need to decode the string because you want to write it to the file as UTF-8, so omit the .decode(...) part:new_file.write(t_w)