title: Unicode writing fail: charmap can't encode character
body: <p>I have a text in which I have a summary. I grab this summary with some regexes as the text always has the same structure.<br>
  Within the summary there is a sentence "NAME is classified as ....", and I have to replace it by a title grabbed in the text and composed of word1 and word2 separated by a comma. As long as I do that, it is working fine (hence I won't add the full code because it is very large and I can't do it, and anyway the problem is not outside of what I will provide.<br>
  I need to add unicode character \u2191 or \u2193 depending on word1 to it , which is associated to a positive or negative value in a dictionary. This has to be done before replacing the sentence.
  My code is basically the following:  </p>
  
  <pre><code>import re
  import io
  file=open(Summaries_file,'a')#also tried open(Summaries_file,'a', encoding="UTF_16_LE") and file=io.open(Summaries_file,'a', encoding="UTF_16_LE")
  code_dict["page"]="Word1\u2191"
  page="page"
  summary = "Data is: 111919919. Name is classified as an infered value".
  print(summary)
  #OUTPUT&gt;"Data is: 111919919. Name is classified as an infered value".
  title= "Word1, Word2"
  
  #this is the part added to regular code&gt;&gt;&gt;&gt;  
  
  titlelist=title.split(",")
  if titlelist[0]==code_dict[page]:
      titlelist[0]=code_dict[page]+"\u2191"
      title=str(titlelist)
      print(titlelist[0])
      #OUTPUT&gt;"Word1↑"#It displays the arrow well
      print(title) #ok, too.
      #OUTPUT&gt;"Word1↑, Word2"
  
   #We go back to the end of the normal code
  insert=re.compile("is classified as")
  print(type(summary))
  #&lt;class 'str'&gt;
  summary=str(insert.sub(title, summary))
  print(summary)
  #OUTPUT&gt;"Data is: 111919919. Name Word1↑, Word2 an infered value".
  
  print("passed")
  file.write(title+'\n')
  file.write(summary+'\n')
  </code></pre>
  
  <p>Then Traceback (most recent call last):  </p>
  
  <pre><code>File "&lt;ipython-input-1-6bc913872cc9&gt;", line 1, in &lt;module&gt;
  runfile('C:/Python Scripts/txtad.py', wdir='C:/Users/Laurent/Documents/Python Scripts')
  
  File "C:\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 699, in runfile
  execfile(filename, namespace)
  
  File "C:\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 88, in execfile
  exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
  
  File "C:/Python Scripts/txtad", line 380, in &lt;module&gt;
  file.write(title+'\n')
  
  File "C:\Anaconda3\lib\encodings\cp1252.py", line 19, in encode
  return codecs.charmap_encode(input,self.errors,encoding_table)[0]
  
  UnicodeEncodeError: 'charmap' codec can't encode character '\u2191' in position 11: character maps to &lt;undefined&gt;
  </code></pre>
  
  <p>Now, I can't figure it out and I am seriously stuck on this.<br>
  I don't know why it fails for writing in the first place since it displays well the signs and that I explicitely encode to the right system in some tests, even opening the file with the right coding.</p>
  
  <p>I tried various things that you can read there:  </p>
  
  <p><a href="https://stackoverflow.com/questions/43706177/solving-error-when-adding-an-unicode-character-to-splits-of-a-string-then-revert?noredirect=1#comment74463879_43706177">https://stackoverflow.com/questions/43706177/solving-error-when-adding-an-unicode-character-to-splits-of-a-string-then-revert?noredirect=1#comment74463879_43706177</a></p>
  
  <p>Indeed the original code is larger, but I tried this and it worked the same way, and input types are strictly identical.</p>
  
  <p>I read these:<br>
  <a href="https://stackoverflow.com/questions/32382686/unicodeencodeerror-charmap-codec-cant-encode-character-u2010-character-m">UnicodeEncodeError: &#39;charmap&#39; codec can&#39;t encode character &#39;\u2010&#39;: character maps to &lt;undefined&gt;</a><br>
  <a href="https://stackoverflow.com/questions/27092833/unicodeencodeerror-charmap-codec-cant-encode-characters">UnicodeEncodeError: &#39;charmap&#39; codec can&#39;t encode characters</a><br>
  <a href="https://stackoverflow.com/questions/5419/python-unicode-and-the-windows-console/32176732#32176732">Python, Unicode, and the Windows console</a><br>
  <a href="https://stackoverflow.com/questions/16346914/python-3-2-unicodeencodeerror-charmap-codec-cant-encode-character-u2013-i">python 3.2 UnicodeEncodeError: &#39;charmap&#39; codec can&#39;t encode character &#39;\u2013&#39; in position 9629: character maps to &lt;undefined&gt;</a><br>
  They are more confusing than anything else, in the end.  </p>
  
  <p>Anyway the problem doesn't lie in the console like those other post since the problem arises with the write instruction which is not displayed, and in addition, the character is well displayed on my console...<br>
  I really can't tell what's going on and how to manage this problem.<br>
  Thanks for your insights.</p>