Wait... WHAT?

Chris Angelico rosuav at gmail.com
Thu Feb 13 02:10:22 CET 2014


On Thu, Feb 13, 2014 at 11:44 AM, Tim Chase
<python.list at tim.thechases.com> wrote:
> Yep:
>
>>>> s = "\u3141" # HANGUL LETTER MIEUM
>>>> f = open('test.txt', 'w')
>>>> f.write("\u3141")
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> UnicodeEncodeError: 'ascii' codec can't encode character '\u3141' in
> position 0: ordinal not in range(128)
>
> Just because the open() call hides the specification of how Python
> should do that encoding doesn't prevent the required encoding from
> happening. :-)
>

All it's hiding is what the default encoding is.

Python 3.4.0rc1+ (default:2ba583191550, Feb 12 2014, 10:30:57)
[GCC 4.7.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> f = open('test.txt', 'w')
>>> f.write("\u3141")
1

Python 3.4.0b2 (v3.4.0b2:ba32913eb13e, Jan  5 2014, 16:23:43) [MSC
v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> f = open('test.txt', 'w')
>>> f.write("\u3141")
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    f.write("\u3141")
  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 character '\u3141' in
position 0: character maps to <undefined>
>>> f = open('test.txt', 'w', encoding='utf-8')
>>> f.write("\u3141")
1

Be explicit, and you can store Unicode strings reliably. That said,
though, you still can't store lists.

ChrisA



More information about the Python-list mailing list