Wait... WHAT?

Tim Chase python.list at tim.thechases.com
Wed Feb 12 19:44:32 EST 2014


On 2014-02-12 23:36, Mark Lawrence wrote:
> On 12/02/2014 22:14, Tim Chase wrote:
> >
> > To be pedantic, you can only write *bytes* to files, so you need
> > to serialize your lists (or other objects) to strings and then
> > encode those to bytes; or skip the string and encode your
> > list/object directly to bytes.
> >
> 
> Really?
> 
>  >>> f = open('test.txt', 'w')
>  >>> f.write('a string')
> 8
>  >>> f.close()
>  >>>

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. :-)

-tkc





More information about the Python-list mailing list