UnicodeEncodeError when not running script from IDE

MRAB python at mrabarnett.plus.com
Tue Feb 12 16:03:19 EST 2013


On 2013-02-12 14:24, Magnus Pettersson wrote:
> I have tried now to take away printing to terminal and just keeping the writing to a .txt file to disk (which is what the scripts purpose is):
>
> with open(filepath,"a") as f:
>      for card in cardlist:
>          f.write(card+"\n")
>
> The file it writes to exists and im just appending to it, but when i run the script trough eclipse, all is fine. When i run in terminal i get this error instead:
>
> File "K:\dev\python\webscraping\kanji_anki.py", line 69, in savefile
>      f.write(card+"\n")
> UnicodeEncodeError: 'ascii' codec can't encode character u'\u898b' in position 3
> 2: ordinal not in range(128)
>
When you open the file, tell it what encoding to use. For example:

with open(filepath, "a", encoding="utf-8") as f:
     for card in cardlist:
         f.write(card + "\n")




More information about the Python-list mailing list