title: 'ascii' codec can't encode character '\xc9' in position 9: ordinal not in range(128)
body: <p>I have a text that I extracted from an image using Tesseract. When I try to print it in the terminal, I get this error <code>'ascii' codec can't encode character '\xc7' in position 10: ordinal not in range(128)</code> in case of special characters (é, è, à, ç ...) 
  When I write the extracted text to a file, I get the correct text including the special characters!<br>
  Here is the code I used:  </p>
  
  <pre><code># -*- coding: utf-8 -*-
  import cv2
  import pytesseract
  with open ('path_to_text_file', 'w', encoding='utf-8') as f:
      try:
          im = cv2.imread(path_to_image)
          text = pytesseract.image_to_string(im, lang='fra')
          f.write(text + '\n')
          print(text)
      except Exception as e:
          print(e)
  f.close()
  </code></pre>
  
  <p>I also tried <code>print(str(text))</code> instead of <code>print(text)</code> but nothing changed!<br>
  In case it is helpful, when I print the type of the variable <code>text</code> (<code>print(type(text))</code>), I get <code>&lt;class 'str'&gt;</code>.
  Any ideas how to fix this error?     </p>
  
  <p><strong>EDIT:</strong>  </p>
  
  <p>Example of the files I am dealing with (Don't worry about confidentiality, this example is from the internet)<br>
  <a href="https://i.stack.imgur.com/DUJGq.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DUJGq.jpg" alt="enter image description here"></a>  </p>
  
  <p>I use Ubuntu 18.04, python 3.6<br>
  The project that I run is on docker.  </p>
  
  <p><strong>EDIT2:</strong><br>
  Output displayed in the terminal:   </p>
  
  <pre><code>'ascii' codec can't encode character '\xc9' in position 1: ordinal not in range(128)
  'ascii' codec can't encode character '\xc9' in position 12: ordinal not in range(128)
  'ascii' codec can't encode character '\xe9' in position 10: ordinal not in range(128)
  30 | Noms BERTHIER
  'ascii' codec can't encode character '\xe9' in position 2: ordinal not in range(128)
  'ascii' codec can't encode character '\u2026' in position 0: ordinal not in range(128)
  Sexe
  Sexe: L N
  3: PARIS 1ER (75)
  ETES
  Taie : 170
  Cruise Her
  | Signature
  Le pol
  du titulaire :
  IDFRABERTHIFR&lt;&lt;EK&lt;KEKKKELELEREREELEREE
  88069231028S8CORINNE&lt;&lt;&lt;&lt;&lt;&lt;&lt;6512068F6  
  </code></pre>
  
  <p>Output written to the text file:  </p>
  
  <p>RÉPUBLIQUE FRANÇAI<br>
  RE<br>
  D'IDENTITÉ Ne : 880692310285<br>
  Nationalité Française<br>
  30 | Noms BERTHIER<br>
  Prénoms): CORINNE<br>
  … Néfel le: 06.12.1985<br>
  Sexe<br>
  Sexe: L N<br>
  3: PARIS 1ER (75)<br>
  ETES<br>
  Taie : 170<br>
  Cruise Her<br>
  | Signature<br>
  Le pol<br>
  du titulaire :<br>
  IDFRABERTHIFR&lt;
  88069231028S8CORINNE&lt;&lt;&lt;&lt;&lt;&lt;&lt;6512068F6   </p>
  
  <p><strong>EDIT3:</strong><br>
  If I remove <code>encoding='utf-8'</code> from <code>with open(filename, 'w') ..</code> I only get the normal characters; every line where there are special characters is not written to the file anymore. 
  python i/o encoding is utf-8
  the output of locale -a is C C.UTF-8 POSIX</p>