UnicodeEncodeError when not running script from IDE

Magnus Pettersson magpettersson at gmail.com
Tue Feb 12 19:20:09 EST 2013


> You don't show the code that actually does the io.open(), nor the 
> 
> url.encode, so I'm not going to guess what you're actually doing.

Hmm im not sure what you mean but I wrote all code needed in a previous post so maybe you missed that one :)
In short I basically just have:
import io
io.open(myfile,"a",encode="UTF-8") as f:
    f.write(my_ustring_with_kanji)

the url.encode() is my unicode string variable named "url" using the type built in  function .encode() which was the thing i wondered why i needed to use, which you explained very well, thank you!

Just one more question since all this is still a little fuzzy in my head.

When do i need to use .decode() in my code? is it when i read lines from f.ex a UTF-8 file? And why didn't I have to use .encode() on my unicode string when running from within eclipse pydev? someone wrote that it has a default codec setting so maybe that handles it for me there (which is kinda dangerous since my programs wont work running outside of eclipse since i didnt do any encoding or using of unicode strings before in my script and it still worked)

--Magnus



More information about the Python-list mailing list