I need to write strings that contains special characters to file. I've tried to use codecs with utf-8 encoding but I still get error:
# -*- coding: utf-8 -*-
import os
import codecs
current_path = os.path.dirname(os.path.abspath(__file__))
with codecs.open(current_path + '/output.txt','w', encoding='utf-8') as f:
a = 'ė'
f.write(a)
f.close()
The exception thrown:
Traceback (most recent call last):
File "/home/francesco/Scrivania/test.py", line 10, in <module>
f.write(a) File "/home/francesco/anaconda2/lib/python2.7/codecs.py", line 706, in write
return self.writer.write(data)
File "/home/francesco/anaconda2/lib/python2.7/codecs.py", line 369, in write
data, consumed = self.encode(object, self.errors)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position 0: ordinal not in range(128)
What's the problem?