python文件编码说明 coding=utf-8
python 支持3种编码声明,一般常用能见到下面两种
1.# -*- coding: utf-8 -*-
这种写法是为了兼容Emacs的编码声明
2.短一点,但Emacs不能用# coding=utf-8
短一点,但Emacs不能用
之所以要声明未编码类型 ,主要是中文出错的问题。
在python 文件开头(一般是第一行或第二行),用来说明你的Python源程序文件用使用的编码。缺省情况下你的程序需要使用ascii码来写,但如果在其中写中文的话,python解释器一般会报错,但如果加上你所用的文件编码,python就会自动处理不再报错。
这里要注意的是:
1.coding 后面使用 ":" 或 "=" 都可以
2.但是, ":" 或 "=" 必须和 coding之间没有空格。之前我就试过有空格声明失败,还是不支持中文。至于 ":" 或 "=" 后面,有没有空格就没所谓了。
见:https://www.python.org/dev/peps/pep-0263/
Defining the Encoding
Python will default to ASCII as standard encoding if no other
encoding hints are given. To define a source code encoding, a magic comment must
be placed into the source files either as first or second
line in the file, such as: # coding=<encoding name> or (using formats recognized by popular editors) #!/usr/bin/python
# -*- coding: <encoding name> -*- or #!/usr/bin/python
# vim: set fileencoding=<encoding name> : More precisely, the first or second line must match the regular
expression "coding[:=]\s*([-\w.]+)". The first group of this
expression is then interpreted as encoding name. If the encoding
is unknown to Python, an error is raised during compilation. There
must not be any Python statement on the line that contains the
encoding declaration.
python文件编码说明 coding=utf-8的更多相关文章
- Python的编码注释# -*- coding:utf-8 -*-
# -*- coding:utf-8 -*-的主要作用是指定文件编码为utf-8, 因为一般默认的是ASCII码,如果要在文件里面写中文,运行时会出现乱码,加上这句之后会把文件编码强制转换为utf-8 ...
- Python文件编码不可以使用UTF16
1. The complete Python source file should use a single encoding. Embedding of differently encoded da ...
- Python 文件编码问题解决
最近使用python操作文件,经常遇到编码错误的问题,例如: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbe in position ...
- python文件编码及执行
兼容中文编码 由于Python源代码也是一个文本文件,所以,当你的源代码中包含中文的时候,在保存源代码时,就需要务必指定保存为UTF-8编码. 当Python解释器读取源代码时,为了让它按UTF-8编 ...
- Python 文件编码(文件乱码)
IndentationError: unindent does not match any outer indentation level 文件未对齐,在记事本打开. 乱码原因:源码文件的编码格式为u ...
- python 文件编码
错误:UnicodeDecodeError: 'gbk' codec can't decode byte 原因:文件为utf-8编码,系统却用gbk去解码,必然出现错误. 解决方法: with ope ...
- 【转】关于Python脚本开头两行的:#!/usr/bin/python和# -*- coding: utf-8 -*-的作用 – 指定文件编码类型
原文网址:http://www.crifan.com/python_head_meaning_for_usr_bin_python_coding_utf-8/ #!/usr/bin/python 是用 ...
- 【转载】关于Python脚本开头两行的:#!/usr/bin/python和# -*- coding: utf-8 -*-的作用 – 指定文件编码类型
1.#!/usr/bin/python 是用来说明脚本语言是 python 的 是要用 /usr/bin下面的程序(工具)python,这个解释器,来解释 python 脚本,来运行 python 脚 ...
- 运行python文件时出错SyntaxError: Non-UTF-8 code starting with '\xb5' in file, but no encoding declared;
今天ytkah在运行python文件时出现错误,提示如下,很明显这是没有定义python文件编码引起的问题,那么要怎么解决呢?很简单,在文件头部定义一下就可以了. File "hello.p ...
随机推荐
- Android编译程序报错:Re-installation failed due to different application signatures.
如果机子上已经安装非本机编译的android程序,在编译的时候就会报错.方法首选的是删除原程序,然后再进行编译. 但是有一部分程序是烧录在系统程序里面的,无法直接删除,这时候可以使用adb shell ...
- nginx的 CPU參数worker_processes和worker_cpu_affinity使用说明
Nginx默认没有开启利用多核CPU,我们能够通过添加worker_cpu_affinity配置參数来充分利用多核CPU.CPU是任务处理,计算最关键的资源,CPU核越多.性能就越好. worker_ ...
- 汇编入门学习笔记 (十二)—— int指令、port
疯狂的暑假学习之 汇编入门学习笔记 (十二)-- int指令.port 參考: <汇编语言> 王爽 第13.14章 一.int指令 1. int指令引发的中断 int n指令,相当于引 ...
- cscope无法索引代码树之外的软链接
http://blog.csdn.net/sudolee/article/details/9052291 背景:为什么非要使用cscope?不用ctags? 尽管ctags可以索引软链接,但是,cta ...
- Unity for Windows: II – Publishing Unity games to Windows Store
原地址:http://digitalerr0r.wordpress.com/2013/08/27/unity-for-windows-ii-publishing-to-windows-8/ Windo ...
- ASP.NET总结
ASP.NET已经学习完.学牛腩的时候面对一些控件和方法会用,但对当中的原理还不懂.学习这部分的内容时, 从头到尾都有一种相识的感觉,把之前一些不懂得地方也理解了,每个知识都有相应的样例练习,学起来还 ...
- 执行pig出错Unhandled internal error. Found interface jline.Terminal, but class was expected
执行pig时报例如以下错误 2015-07-14 10:41:12,869 [main] ERROR org.apache.pig.Main - ERROR 2998: Unhandled inter ...
- mosquitto ---SSL/TLS 单向认证+双向认证
生成证书 # * Redistributions in binary form must reproduce the above copyright # notice, this list of ...
- php扩展swoole的安装
这个明星php安装是要装php-pear yum install php-pear 然后通过pear命名安装swoole pecl install swoole 配置php.ini 添加 extens ...
- JVM虚拟机(四):JVM 垃圾回收机制概念及其算法
垃圾回收概念和其算法 谈到垃圾回收(Garbage Collection)GC,需要先澄清什么是垃圾,类比日常生活中的垃圾,我们会把他们丢入垃圾箱,然后倒掉.GC中的垃圾,特指存于内存中.不会再被使用 ...