1

Code

# -*- coding: ISO-8859-15 -*-
import sys
import codecs
filename2 = "log_unicode2.log"
log_file2 = codecs.open(filename2, "w", "utf-8")
sys.stdout = log_file2
log_file2.write('aééé')

Error

Traceback (most recent call last):
  File "snippet_problem_unicode.py", line 7, in <module>
    log_file2.write('a├®├®├®')
  File "C:\Users\dev1\Envs\atao\lib\codecs.py", line 691, in write
    return self.writer.write(data)
  File "C:\Users\dev1\Envs\atao\lib\codecs.py", line 351, in write
    data, consumed = self.encode(object, self.errors)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1: ordinal
not in range(128)

Contexte

  • Windows 7
  • Python 2.6
  • Eclipse

'aééé' is a byte string (latin-1) which need to be converted to utf-8. Why do this conversion involve ascii codec ?

1 Answer 1

1

You are writing a byte string to a file object expecting a unicode value. To go from byte string to unicode value Python has to decode the bytestring. This decoding uses the default ASCII codec.

Either:

  1. Use a unicode literal instead of a byte string:

    log_file2.write(u'aééé')
    
  2. Explicitly decode the bytestring to Unicode first, using your source file encoding:

    log_file2.write('aééé'.decode('latin1'))
    
  3. Not use codecs.open() but open the file using the built-in open() function instead, then manually decode, then encode to UTF:

    log_file2 = open(filename2, "w")
    log_file2 .write('aééé'.decode('latin1').encode('utf8')
    

    or use a unicode literal and encode manually:

    log_file2 = open(filename2, "w")
    log_file2 .write(u'aééé'.encode('utf8'))
    
Sign up to request clarification or add additional context in comments.

2 Comments

Said diffrently : a byte string is implicitely the result of a former ascii encoding and is decoded as such by the codecs lib.
@LBarret: a byte string may contain textual information. When attempting to use it as Unicode text it'll be decoded with the assumption it is using the ASCII codec.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.