I am on Linux and a want to write string (in utf-8) to txt file. This is my code:
# -*- coding: UTF-8-*-
import os
import sys
def __init__(self, dirname, speaker, file, exportFile):
text_file = open(exportFile, "a")
text_file.write(speaker.encode("utf-8"))
text_file.write(file.encode("utf-8"))
text_file.close()
When I am on Windows, it works. But on Linux, I get this error:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position in position 36: ordinal not in range(128)
How can I solve this problem? Thank you.
decode('utf-8')to convert your utf8 string to a bytestring?"au"mode?"ab"(binary mode), which is described in documentation to avoid text mode?