Wait... WHAT?

Mark Lawrence breamoreboy at yahoo.co.uk
Thu Feb 13 01:59:16 CET 2014


On 13/02/2014 00:44, Tim Chase wrote:
> 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
>
>

Which clearly reinforces the fact that what you originally said is 
incorrect, I don't have to do anything, Python very kindly does things 
for me under the covers.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com





More information about the Python-list mailing list