python33, windows, UnicodeEncodeError: 'charmap' codec can't encode characters in position, to print out the file contents to stdout,

wxjmfauth at gmail.com wxjmfauth at gmail.com
Sun Jul 6 20:14:38 CEST 2014


Le dimanche 6 juillet 2014 18:53:34 UTC+2, Rick Johnson a écrit :

[...]
> 
> 
> Seems like she'd better do the decoding before printing

No

> [...], or
> 
> am i wrong again?

Yes


>>> s = 'abc需'
>>> sys.stdout.encoding
'<unicode>'
>>> print(s)
abc需
>>> sys.stdout.encoding = 'cp437'
>>> sys.stdout.encoding
'cp437'
>>> print(s)
Traceback (most recent call last):
  File "<eta last command>", line 1, in <module>
  File "D:\jm\jmpy\eta\eta40beta2\etastdio.py", line 158, in write
    s = s.encode(self.pencoding).decode('cp1252')
  File "C:\Python32\lib\encodings\cp437.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode characters in position 4-5: 
character maps to <undefined>
>>> print(s.encode(sys.stdout.encoding, 'replace'))
'abcé??'
>>> sys.stdout.encoding = 'cp850'
>>> sys.stdout.encoding
'cp850'
>>> print(s)
Traceback (most recent call last):
  File "<eta last command>", line 1, in <module>
  File "D:\jm\jmpy\eta\eta40beta2\etastdio.py", line 158, in write
    s = s.encode(self.pencoding).decode('cp1252')
  File "C:\Python32\lib\encodings\cp850.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode characters in position 4-5: 
character maps to <undefined>
>>> print(s.encode(sys.stdout.encoding, 'replace'))
'abcé??'
>>> # and so on
>>> sys.stdout.encoding = 'cp1252')
  File "<eta last command>", line 1
    sys.stdout.encoding = 'cp1252')
                                  ^
SyntaxError: invalid syntax
>>> # oops
>>> sys.stdout.encoding = 'cp1252'
>>> print(s)
abc需
>>> sys.stdout.encoding = 'mac-roman'
>>> print(s)
abcŽÏÛ
>>> print(s.encode(sys.stdout.encoding, 'replace'))
'abc需'
>>> sys.stdout.encoding = 'utf-8'
>>> print(s.encode(sys.stdout.encoding, 'replace'))
'abc需'
>>> sys.stdout.encoding = 'utf-16-le'
>>> print(s.encode(sys.stdout.encoding, 'replace'))
'abc需'
>>> 
>>> sys.stdout.encoding = 'utf-32-be'
>>> print(s.encode(sys.stdout.encoding, 'replace'))
'abc需'
>>>

jmf



More information about the Python-list mailing list