Getting "UnicodeEncodeError: 'charmap' codec can't encode character" when saving to a text file in Windows
Getting "UnicodeEncodeError: 'charmap' codec can't encode character" when saving to a text file in Windows
我在 Windows 7 上使用 Python 3.4。我的程序生成一些数字(范围 0-255),然后将它们转换为 ascii 字符 (chr) 并创建一个字符串。现在我想将这个字符串的内容保存在一个文本文件中。它给了我以下错误:
UnicodeEncodeError: 'charmap' codec can't encode character '\x8e' in position 6: character maps to <undefined>
请注意,字符串的长度是可变的,并且可以出现任何和所有代码 (0-255)。
示例代码:
file = open('somefiliename.txt', 'w')
file.write(result) #result being the string variable containing ascii chars.
file.close()
我可以打印结果字符串并且使用 print(result) 没有错误。但是它没有保存到文件中。
结果=''
对于范围内的 y(4):
对于范围内的 x(4):
结果 += chr(矩阵[x, y])
打印(结果)
代码比较长,我在上面补充了相关的。 matrix 是一个 numpy 二维 (4x4) 矩阵,用于存储数字。
我可以在 Windows 7 中重现这个,使用像 -
这样的简单代码
>>> 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>
而第129位的字符从\x81
开始,以此类推
出现此问题是因为您使用默认编码打开文件,如果您真的想将这些字符写入文件,您应该使用 utf8
编码打开它,同时指定换行符参数 ''
(为什么?下面解释)。例子-
>>> f = open('a.txt','w',encoding="utf8",newline='')
>>> f.write(s)
257
>>> f.close()
对于那些使用 Python 2.x 的用户,他们可以使用 codecs.open()
打开具有特定编码的文件。
同样对于 Python 3.x ,您在回读此文件时会遇到问题,因为在回读时您会看到 ASCII 值 13 - (Carriage return - '\r'
) 已转换为 '\n'
) 。这是因为在 Python 3.x 中,如果我们不为 open()
函数指定换行参数(这意味着它是 None),它将使用通用换行符(这将将所有 - \r\n
、 \r
、 \n
转换为 \n
) 。从 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:
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.
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.
在您的情况下,您应该在写入和读取文件时指定 newline=''
参数。
阅读示例-
>>> f= open('b.txt','r',newline='',encoding='utf8')
>>> x = f.read()
>>> print(x)
我在 Windows 7 上使用 Python 3.4。我的程序生成一些数字(范围 0-255),然后将它们转换为 ascii 字符 (chr) 并创建一个字符串。现在我想将这个字符串的内容保存在一个文本文件中。它给了我以下错误:
UnicodeEncodeError: 'charmap' codec can't encode character '\x8e' in position 6: character maps to <undefined>
请注意,字符串的长度是可变的,并且可以出现任何和所有代码 (0-255)。
示例代码:
file = open('somefiliename.txt', 'w')
file.write(result) #result being the string variable containing ascii chars.
file.close()
我可以打印结果字符串并且使用 print(result) 没有错误。但是它没有保存到文件中。
结果='' 对于范围内的 y(4): 对于范围内的 x(4): 结果 += chr(矩阵[x, y]) 打印(结果)
代码比较长,我在上面补充了相关的。 matrix 是一个 numpy 二维 (4x4) 矩阵,用于存储数字。
我可以在 Windows 7 中重现这个,使用像 -
这样的简单代码>>> 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>
而第129位的字符从\x81
开始,以此类推
出现此问题是因为您使用默认编码打开文件,如果您真的想将这些字符写入文件,您应该使用 utf8
编码打开它,同时指定换行符参数 ''
(为什么?下面解释)。例子-
>>> f = open('a.txt','w',encoding="utf8",newline='')
>>> f.write(s)
257
>>> f.close()
对于那些使用 Python 2.x 的用户,他们可以使用 codecs.open()
打开具有特定编码的文件。
同样对于 Python 3.x ,您在回读此文件时会遇到问题,因为在回读时您会看到 ASCII 值 13 - (Carriage return - '\r'
) 已转换为 '\n'
) 。这是因为在 Python 3.x 中,如果我们不为 open()
函数指定换行参数(这意味着它是 None),它将使用通用换行符(这将将所有 - \r\n
、 \r
、 \n
转换为 \n
) 。从 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:
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.
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.
在您的情况下,您应该在写入和读取文件时指定 newline=''
参数。
阅读示例-
>>> f= open('b.txt','r',newline='',encoding='utf8')
>>> x = f.read()
>>> print(x)