Getting "UnicodeEncodeError: 'charmap' codec can't encode character" when saving to a text file in Windows

Abdul Qadir

I am using Python 3.4 on Windows 7. My program generates some numbers (range 0-255) and then converts them into ascii characters (chr) and creates a string. Now I want to save contents of this string in a text file. It gives me the following error:

UnicodeEncodeError: 'charmap' codec can't encode character '\x8e' in position 6: character maps to <undefined>

Please note that the length of the string is variable and any and all codes (0-255) can occur.

Sample code:

file = open('somefiliename.txt', 'w')
file.write(result) #result being the string variable containing ascii chars.
file.close()

I can print the result string and there is no error using print(result). But it is not saving to a file.

result = '' for y in range(4): for x in range(4): result += chr(matrix[x, y]) print(result)

The code is pretty long, i have added above the pertinent. matrix is a numpy 2-dimensional (4x4) matrix which stores the numbers.

Anand S Kumar

I can reproduce this in Windows 7, using a simple code like -

>>> s = ''
>>> for i in range(256):
...     s += chr(i)
...
>>>
>>> f = open('a.txt','w')
>>> f.write(s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>

  File "C:\Python34\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 129-160: character maps to <undefined>

And the characters in position 129 start at \x81 , etc.

The issue occurs because you are openning your file with a default encoding, if you really want to write those characters into your file, you should open it with utf8 encoding , also specify the newline argument to '' (Why? Explained below). Example -

>>> f = open('a.txt','w',encoding="utf8",newline='')
>>> f.write(s)
257
>>> f.close()

For those using Python 2.x , they can use codecs.open() to open the file with a specific encoding.


Also for Python 3.x , you would have issues when reading back this file, as when reading back you would see the ASCII value 13 - (Carriage return - '\r') has been converted to '\n') . This is because in Python 3.x , if we do not specify the newline argument for open() function (which means it is None), it will use universal newline (which will convert all - \r\n , \r , \n to \n) . From documentation -

newline controls how universal newlines works (it only applies to text mode). It can be None, '', '\n', '\r', and '\r\n'. It works as follows:

  1. On input, if newline is None, universal newlines mode is enabled. Lines in the input can end in '\n', '\r', or '\r\n', and these are translated into '\n' before being returned to the caller. If it is '', universal newline mode is enabled, but line endings are returned to the caller untranslated. If it has any of the other legal values, input lines are only terminated by the given string, and the line ending is returned to the caller untranslated.

  2. On output, if newline is None, any '\n' characters written are translated to the system default line separator, os.linesep. If newline is '', no translation takes place. If newline is any of the other legal values, any '\n' characters written are translated to the given string.

In your case, you should specify newline='' argument while both writing as well as reading the file.

Example of reading -

>>> f= open('b.txt','r',newline='',encoding='utf8')
>>> x = f.read()
>>> print(x)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Saving a data structure to a text file

From Dev

Run shell script when saving a file in Sublime Text 3

From Dev

Access Denied when saving a file, Windows 8 App

From Dev

Saving output of a function to a text file

From Dev

trouble with saving ndarray to text file

From Dev

Saving textbox text into XML file

From Dev

Getting a 400 when saving metadata

From Dev

Saving a batch variable in a text file

From Dev

Saving a file as .RProfile in windows

From Dev

Saving and formatting text file with Matlab

From Dev

Refreshing and Saving as a text file in Excel

From Dev

Saving PairedRDD as a text file

From Dev

UnicodeEncodeError: 'charmap' codec can't encode character '\u2010': character maps to <undefined>

From Dev

VB Getting The SubFolder name and saving it to a Text file

From Dev

Saving text file from code

From Dev

Saving ArrayList to Text File

From Dev

Saving user feedback in a text file

From Dev

UnicodeEncodeError: 'charmap' codec can't encode character (in Python 3.3)

From Dev

Access Denied when saving a file, Windows 8 App

From Dev

trouble with saving ndarray to text file

From Dev

from DataGridView saving to text file issue when saving date values on vb.net

From Dev

VB Getting The SubFolder name and saving it to a Text file

From Dev

Permission denied when saving to file as text, a

From Dev

Saving ArrayList to Text File

From Dev

UnicodeEncodeError: 'charmap' codec can't encode character : character maps to <undefined>

From Dev

UnicodeEncodeError: 'charmap' codec can't encode character '\u2080' in position 28: character maps to <undefined>

From Dev

getting input from click event in javascript and sending it to django or saving it as text file

From Dev

UnicodeEncodeError: 'charmap' codec can't encode character '\u2264'

From Dev

python 3 - UnicodeEncodeError: 'charmap' codec can't encode character (Encode so it's in a file)

Related Related

  1. 1

    Saving a data structure to a text file

  2. 2

    Run shell script when saving a file in Sublime Text 3

  3. 3

    Access Denied when saving a file, Windows 8 App

  4. 4

    Saving output of a function to a text file

  5. 5

    trouble with saving ndarray to text file

  6. 6

    Saving textbox text into XML file

  7. 7

    Getting a 400 when saving metadata

  8. 8

    Saving a batch variable in a text file

  9. 9

    Saving a file as .RProfile in windows

  10. 10

    Saving and formatting text file with Matlab

  11. 11

    Refreshing and Saving as a text file in Excel

  12. 12

    Saving PairedRDD as a text file

  13. 13

    UnicodeEncodeError: 'charmap' codec can't encode character '\u2010': character maps to <undefined>

  14. 14

    VB Getting The SubFolder name and saving it to a Text file

  15. 15

    Saving text file from code

  16. 16

    Saving ArrayList to Text File

  17. 17

    Saving user feedback in a text file

  18. 18

    UnicodeEncodeError: 'charmap' codec can't encode character (in Python 3.3)

  19. 19

    Access Denied when saving a file, Windows 8 App

  20. 20

    trouble with saving ndarray to text file

  21. 21

    from DataGridView saving to text file issue when saving date values on vb.net

  22. 22

    VB Getting The SubFolder name and saving it to a Text file

  23. 23

    Permission denied when saving to file as text, a

  24. 24

    Saving ArrayList to Text File

  25. 25

    UnicodeEncodeError: 'charmap' codec can't encode character : character maps to <undefined>

  26. 26

    UnicodeEncodeError: 'charmap' codec can't encode character '\u2080' in position 28: character maps to <undefined>

  27. 27

    getting input from click event in javascript and sending it to django or saving it as text file

  28. 28

    UnicodeEncodeError: 'charmap' codec can't encode character '\u2264'

  29. 29

    python 3 - UnicodeEncodeError: 'charmap' codec can't encode character (Encode so it's in a file)

HotTag

Archive