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.
io.open(filename, "w")toio.open(filename, "wb"). notice thebnext to thew