简体   繁体   中英

utf-8 encoding works in iPython console, not in Spyder editor

here is the minimal test. in Spyder Editor, I wrote these 4 lines (nothing else)

# -*- coding: utf-8 -*-
import sys
print(sys.getdefaultencoding() )
print(u"µ")

I get:

utf-8
µ

but if I just copy-paste the 2 lines in iPython console, I get

utf-8 \n µ

what's wrong with my.py file or with spyder Editor?

what's wrong with my.py file or with spyder Editor?

The problem is most likely that you're using Windows and the console interprets your output as Windows-1252 (the default console encoding for western european windows):

>>> 'µ'.encode('utf-8').decode('windows-1252')
'µ'

It's not clear whether the first snippet is the spyder console or a normal python shell in the windows console.

 print(sys.getdefaultencoding() )

has literally nothing to do with anything. sys.getdefaultencoding() is the internal python encoding.

You can try to mess with PYTHONIOENCODING , or see what locale.getpreferredencoding() returns.

I solved the problem, thanks to @Carlos Cordoba (Spyder's master).

I opened the file in notepad++ , and converted it into " UTF-8 BOM " (it was in UTF-8 but without BOM)

Now it works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM