python3默认编码格式_哪些文件编码支持Python 3源文件?

1586010002-jmsa.png

Before you go telling me to read PEP 0263, keep reading...

I can't find any documentation that details which file encodings are supported for Python 3 source files.

I've found hundreds (thousands?) of questions, answers, posts, emails, etc. about how to declare - at the top of your source file - the encoding of that source file, but none of them answer my question. Bear with me and imagine doing (or actually try) the following:

Open Notepad (I'm using regular old Notepad on Windows 7, but I doubt it matters; I'm sure your superior editor can do something similar.)

Type your favorite line of Python code ( I used print( 'Hello, world!' ) )

Select "File" -> "Save"

Select a folder and file name ( I used "E:\Temp\hello.py" )

Change the "Encoding:" setting from the default "ANSI" to "Unicode"

Press "Save"

Open a command prompt, change to the folder containing your new file, and try to run it

Here's the output I get:

E:\Temp>python --version

Python 3.4.1

E:\Temp>python "hello.py"

File "hello.py", line 1

SyntaxError: Non-UTF-8 code starting with '\xff' in file hello.py on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

Now, when I open this same file in Notepad++ and look at the "Encoding" menu, it has the option "Encode in UCS-2 Little Endian" selected. Wikipedia tells me that this is basically UTF-16 encoding. Whatever. I don't really care. More research reveals that my editor has inserted a two-byte BOM (Byte Order Mark) with a value of '\xff\xfe' at the front of the file to indicate the file encoding. So at least I know where the '\xff' code that Python is complaining about comes from.

So I go and read PEP 0263 - and everything else regarding it - on the web, and I try adding a comment like this to the first line of the file

# coding: utf-16

with all sorts of different values for the encoding, and nothing helps. But it can't help, right? Because Python isn't even getting as far as my encoding declaration; It's choking on the first byte of the source file!

So what I really want to know is...

Why can't the Python 3 interpreter read this file?

If "Unicode" or "UCS-2 Little Endian" or "UTF-16" or whatever isn't supported, what is???

P.S. I even found another question on StackOverflow which seems to be the exact issue I'm having, but it was closed - erroneously in my opinion - as a duplicate. :(

--- EDIT ---

Someone asked for my "compiled options". Here's some output. Maybe it will help?

E:\Temp>python

Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit (Intel)] on win32

Type "help", "copyright", "credits" or "license" for more information.

>>> import sysconfig

>>> print( sysconfig.get_config_vars() )

{'EXT_SUFFIX': '.pyd', 'srcdir': 'C:\\Python34', 'py_version_short': '3.4', 'base': 'C:\\Python34', 'prefix': 'C:\\Python34', 'projectbase': 'C:\\Python34', 'INCLUDEPY': 'C:\\Python34\\Include', 'platbase': 'C:\\Python34', 'py_version_nodot': '34', 'exec_prefix': 'C:\\Python34', 'EXE': '.exe', 'installed_base': 'C:\\Python34', 'SO': '.pyd', 'installed_platbase': 'C:\\Python34', 'VERSION': '34', 'BINLIBDEST': 'C:\\Python34\\Lib', 'LIBDEST': 'C:\\Python34\\Lib', 'userbase': 'C:\\Users\\alonghi\\AppData\\Roaming\\Python', 'py_version': '3.4.1', 'abiflags': '', 'BINDIR': 'C:\\Python34'}

>>>

解决方案

A source encoding must be:

An encoding supported by the version of Python in question. (This varies by version and platform, for example you only get mbcs on Windows.)

Loosely ASCII-compatible, enough that the # coding: declaration can be read using ascii which is the initial source encoding before any declaration is read. See PEP0263 ‘Concepts’ item 1.

The encoding that Windows misleadingly calls “Unicode”, UTF-16LE, is not ASCII-compatible (and generally is a barrel of problems you should try to avoid using). Python would need special encoding-specific support to detect UTF-16 source files and this feature has been declined for now.

The # coding: you should use is almost invariably UTF-8.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值