Write in a file with Python 3 & unicode Write in a file with Python 3 & unicode python-3.x python-3.x

Write in a file with Python 3 & unicode


Your symptoms look like regular a "UTF-8 as latin-1" problem.

Have you checked what encoding is used on the software that you are using to view that file? I'd say that the problem is not necessarily in your python code but in the viewer.

If you create a file with your example text Une déclaration à faire... using UTF-8 encoding and then read that file interpreting the contents using encoding ISO-8859-1 or windows-1252, then the result is shown as the output you described: Une déclaration à faire....

Also, in python 3 the default source encoding is UTF-8. http://www.python.org/dev/peps/pep-3120/


It seems python does not understand what encoding your source is in. You have to tell it, either by using byte-order mark (preferred) or via declaring the type with special comment in first or second line. So:

  1. tell your editor to store byte-order-mark in the source, or, if it can't do that
  2. put a comment at the beginning containing (IIRC) encoding: utf-8.