Question

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.

No correct solution

OTHER TIPS

You could try to use the "codecs" module:

import codecs

with codecs.open('filename', 'w', encoding='utf-8') as out:  
    out.write(u'some text')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top