2

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?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Hyperion
  • 2,515
  • 11
  • 37
  • 59
  • 3
    Note that you don't need the `f.close()` call, the `with` statement already signals to the file object that the block has ended, triggering a file close at that point. – Martijn Pieters Apr 20 '16 at 11:06
  • 1
    `can't encode character u'...'` - that sounds like python2, so `a` is a binary string, not unicode, which a file opened with `codecs.open` and a specified encoding would expect. Try `a = u'ė'` instead. – mata Apr 20 '16 at 11:11
  • Now the problem is that you are not writing a Unicode string. You are trying to write (undecoded) bytes, so Python tries to first *decode* the data to Unicode before it can encode to UTF-8. – Martijn Pieters Apr 20 '16 at 11:15
  • @Martijn Pieters Yeah, you are right, I already deleted my comment; as I wrote it was year ago or so ... :) I remember having issue, but don't remember solution, but I solved it :) can check when get home – Drako Apr 20 '16 at 11:19
  • You may find this article helpful: [Pragmatic Unicode](http://nedbatchelder.com/text/unipain.html), which was written by SO veteran Ned Batchelder. – PM 2Ring Apr 20 '16 at 11:23
  • Do you have any specific reason to code in Python 2.7? You wouldn't have this issue in Python 3. – Roberto Apr 20 '16 at 11:37

0 Answers0