How to: Declare encoding UTF-8 in python
References:
http://stackoverflow.com/questions/12238307/declaring-encoding-in-python
http://stackoverflow.com/questions/3170211/why-declare-unicode-by-string-in-python
Following explanations are excerpted from above links, this is only used for knowledge sharing:)
Put:
# -*- coding: UTF-8 -*-
as the first line of the file (or second line if using *nix) and save the file as UTF-8.
If you're using Python 2, use Unicode string literals (u"..."), for example:
means = u"a ، b ، c, myCompany™"
lst = means.split(u"،")
When you specify # -*- coding: utf-8 -*-, you're telling Python the source file you've saved is utf-8. The default for Python 2 is ASCII (for Python 3 it's utf-8). This just affects how the interpreter reads the characters in the file.
In general, it's probably not the best idea to embed high unicode characters into your file no matter what the encoding is; you can use string unicode escapes, which work in either encoding.
When you declare a string with a u in front, like u'This is a string', it tells the Python compiler that the string is Unicode, not bytes. This is handled mostly transparently by the interpreter; the most obvious difference is that you can now embed unicode characters in the string (that is, u'\u2665' is now legal). You can use from __future__ import unicode_literals to make it the default.
This only applies to Python 2; in Python 3 the default is Unicode, and you need to specify a b in front (like b'These are bytes', to declare a sequence of bytes).
How to: Declare encoding UTF-8 in python的更多相关文章
- SyntaxError: Non-UTF-8 code starting with '\xe5' in file ***.py on line 105, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for
用charles抓包时, 对抓到的html,放到pycharm中解析, 结果报错: SyntaxError: Non-UTF-8 code starting with '\xe5' in file * ...
- SyntaxError: Non-ASCII character '\xe5' in file index.py on line 6, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
python入门,hhh 在慕课网上学习python入门,编写汉诺塔的递归调用时,代码正确.但是加上注释后编译不通过 报如下错误: SyntaxError: Non-ASCII character , ...
- SyntaxError: Non-ASCII character 'æ' in file csdn.py on line 7, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
错误信息: SyntaxError: Non-ASCII character , but no encoding declared; see http://python.org/dev/peps/pe ...
- SyntaxError: Non-UTF-8 code starting with '\xbb' in file D:\流畅学python\ex32.py on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
1. 报错如下: SyntaxError: Non-UTF-8 code starting with '\xd3' in file D:\流畅学python\ex34.py on line 4, bu ...
- python2.7报错Non-ASCII character '\xe5' in file knn.py on line 3, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
Python默认是以ASCII作为编码方式的,如果在自己的Python源码中包含了中文(或者其他非英语系的语言),此时即使你把自己编写的Python源文件以UTF-8格式保存了,但实际上,这依然是不行 ...
- 【LeetCode】820. 单词的压缩编码 Short Encoding of Words(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode-cn.com/problems/short- ...
- SyntaxError: Non-UTF-8 code starting with '\xc5' in file t.py on line 3,but no encoding declared;see http://python.org/dev/peps/pep-0263/ for details
解决方案是: 在程序最上面加上:# coding=gbk 这样程序就可以正常运行了.
- Python报错:SyntaxError: Non-ASCII character '\xe5' in file 1.py on line 6, but no encoding declared...
本文由荒原之梦原创,原文链接:http://zhaokaifeng.com/?p=686 具体报错内容: File "1.py", line 6 SyntaxError: Non- ...
- 运行python文件时出错SyntaxError: Non-UTF-8 code starting with '\xb5' in file, but no encoding declared;
今天ytkah在运行python文件时出现错误,提示如下,很明显这是没有定义python文件编码引起的问题,那么要怎么解决呢?很简单,在文件头部定义一下就可以了. File "hello.p ...
随机推荐
- 射击的乐趣:WIN32诠释打飞机游戏源码补充
打飞机游戏源码补充 从指定位置加载bmp并显示到对话框. , TRUE);, , LR_LOADFROMFILE); { BITMAP bmpinfo; ...
- ecshop如何去除后台左侧云服务中心菜单
介绍一下如何去除后台云服务中心菜单: 打开admin/templates/menu.htm,把539行的 document.getElementById("menu-ul").in ...
- Css基础-介绍及语法
css 文件后缀.css 基础语法: selector { property:value } 例如: h1 {color:red;font-size:14px;} color:字体颜色 font-s ...
- cocos2d-x 手势之简单实现
转自:http://blog.sina.com.cn/s/blog_61ece099010187tl.html 手势之前也发过一篇,但是我感觉那个还不够轻巧. 而且大多数游戏里面不会有那么复杂的手势, ...
- Android AndroidManifest 清单文件以及权限具体解释
每一个Android应用都须要一个名为AndroidManifest.xml的程序清单文件,这个清单文件名称是固定的而且放在每一个Android应用的根文件夹下.它定义了该应用对于Android系统来 ...
- httpclient4.3 工具类
httpclient4.3 java工具类. .. .因项目须要开发了一个工具类.正经常常使用的httpclient 请求操作应该都够用了 工具类下载地址:http://download.csdn. ...
- C#-获取datagriview选中行中某一列的值
获取选中行中某一列的值: int index = dg_Product.CurrentRow.Index; //取得选中行的索引 txt_ProductId.Text = dg_Product.Row ...
- New Features in C# 3.0, 4.0 and 5.0 (英文差的免入)
What’s New in C# 3.0 Language Integrated Query(LINQ) - LINQ enables queries to be written in C# pr ...
- CDOJ 1157 数列(seq) 分块+线段树
数列(seq) Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/1157 Desc ...
- C栈stack
栈是一种 特殊的线性表 栈仅能在线性表的一端进行操作 栈顶(Top):允许操作的一端 栈底(Bottom):不允许操作的一端 Stack的常用操作 创建栈 销毁栈 清空栈 进栈 出栈 获取栈顶元素 ...