Douwe
Douwe

Reputation: 39

How do I write and encode my file in UTF-8 style in python?

import io

def write_ngrams(table, filename):

    with io.open(filename, "w") as file:
        for i in table:
            outputstring=(('%d %s\n' % (table[i], i)))
            encoded = outputstring.encode("utf-8")
            file.write(encoded)

tabel = ngram_table('hiep, hiep, hoera!', 3, 0) // these are not really interesting for now

write_ngrams(tabel, "testfile3.txt")

I am getting an error at the file.write(encoded) line that states the following:

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

However my assignment was: The output must use the utf8 encoding,

Which means that the output should be in the form of b'....'

With the ways I have tried I only get the string without the encoding or the error. However when I use print(encoded) I do receive the output in UTF-8 encoding, however when I write it to a file the encoding is gone or I get an error.

Any tips would be appreciated.

Upvotes: 0

Views: 216

Answers (1)

rdas
rdas

Reputation: 21275

You can pass the string to write() & open the file with the encoding set to utf-8

import io

def write_ngrams(table, filename):

    with io.open(filename, "w", encoding='utf-8') as file:
        for i in table:
            outputstring=(('%d %s\n' % (table[i], i)))
            file.write(outputstring)

tabel = ngram_table('hiep, hiep, hoera!', 3, 0) // these are not really interesting for now

write_ngrams(tabel, "testfile3.txt")

Upvotes: 3

Related Questions