如何在Python 中使用UTF-8 编码 && Python 使用 注释,Python ,UTF-8 编码 , Python  注释

PIP

$ pip install beautifulsoup4

$ python -m pip install --upgrade pip



PyCharm 设置 Python Script 模板内容:
创建.py文件时自动添加 #coding utf8 文件头
File > Settings > Editor > File and Code Templates > Python Script>
#coding utf8

参考图片:http://img.imooc.com/57d6c0eb0001d66d05000305.jpg
参考链接:http://www.imooc.com/qadetail/127992

1

1

python spider demo:

# coding:utf8
__author__ = 'xray'
import urllib2
import cookielib url = "https://rollbar.com/docs/" print '第一种方法'
response1 = urllib2.urlopen(url)
print response1.getcode()
print len(response1.read()) print '第二种方法'
request = urllib2.Request(url)
request.add_header("user-agent", "Mozilla/5.0")
response2 = urllib2.urlopen(request)
print response2.getcode()
print response2.read() print '第三种方法'
cj = cookielib.CookiJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
urllib2.install_opener(opener)
response3 = urllib2.urlopen(url)
print response3.getcode()
print cj
print response3.read()

zh-CN error:

1

1

如何在Python 中使用UTF-8 编码 && Python 使用 注释

PEP 263 -- Defining Python Source Code Encodings

PEP: 263
Title: Defining Python Source Code Encodings
Author: Marc-André Lemburg <mal at lemburg.com>, Martin von Löwis <martin at v.loewis.de>
Status: Final
Type: Standards Track
Created: 06-Jun-2001
Python-Version: 2.3
Post-History:  

Abstract

    This PEP proposes to introduce a syntax to declare the encoding of
a Python source file. The encoding information is then used by the
Python parser to interpret the file using the given encoding. Most
notably this enhances the interpretation of Unicode literals in
the source code and makes it possible to write Unicode literals
using e.g. UTF-8 directly in an Unicode aware editor.

Problem

    In Python 2.1, Unicode literals can only be written using the
Latin-1 based encoding "unicode-escape". This makes the
programming environment rather unfriendly to Python users who live
and work in non-Latin-1 locales such as many of the Asian
countries. Programmers can write their 8-bit strings using the
favorite encoding, but are bound to the "unicode-escape" encoding
for Unicode literals.

Proposed Solution

    I propose to make the Python source code encoding both visible and
changeable on a per-source file basis by using a special comment
at the top of the file to declare the encoding. To make Python aware of this encoding declaration a number of
concept changes are necessary with respect to the handling of
Python source code data.

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 "^[ \t\v]*#.*?coding[:=][ \t]*([-_.a-zA-Z0-9]+)".
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. If the first line matches the second line
is ignored. To aid with platforms such as Windows, which add Unicode BOM marks
to the beginning of Unicode files, the UTF-8 signature
'\xef\xbb\xbf' will be interpreted as 'utf-8' encoding as well
(even if no magic encoding comment is given). If a source file uses both the UTF-8 BOM mark signature and a
magic encoding comment, the only allowed encoding for the comment
is 'utf-8'. Any other encoding will cause an error.

Examples

    These are some examples to clarify the different styles for
defining the source code encoding at the top of a Python source
file: 1. With interpreter binary and using Emacs style file encoding
comment: #!/usr/bin/python
# -*- coding: latin-1 -*-
import os, sys
... #!/usr/bin/python
# -*- coding: iso-8859-15 -*-
import os, sys
... #!/usr/bin/python
# -*- coding: ascii -*-
import os, sys
... 2. Without interpreter line, using plain text: # This Python file uses the following encoding: utf-8
import os, sys
... 3. Text editors might have different ways of defining the file's
encoding, e.g. #!/usr/local/bin/python
# coding: latin-1
import os, sys
... 4. Without encoding comment, Python's parser will assume ASCII
text: #!/usr/local/bin/python
import os, sys
... 5. Encoding comments which don't work: Missing "coding:" prefix: #!/usr/local/bin/python
# latin-1
import os, sys
... Encoding comment not on line 1 or 2: #!/usr/local/bin/python
#
# -*- coding: latin-1 -*-
import os, sys
... Unsupported encoding: #!/usr/local/bin/python
# -*- coding: utf-42 -*-
import os, sys
...

Concepts

    The PEP is based on the following concepts which would have to be
implemented to enable usage of such a magic comment: 1. The complete Python source file should use a single encoding.
Embedding of differently encoded data is not allowed and will
result in a decoding error during compilation of the Python
source code. Any encoding which allows processing the first two lines in the
way indicated above is allowed as source code encoding, this
includes ASCII compatible encodings as well as certain
multi-byte encodings such as Shift_JIS. It does not include
encodings which use two or more bytes for all characters like
e.g. UTF-16. The reason for this is to keep the encoding
detection algorithm in the tokenizer simple. 2. Handling of escape sequences should continue to work as it does
now, but with all possible source code encodings, that is
standard string literals (both 8-bit and Unicode) are subject to
escape sequence expansion while raw string literals only expand
a very small subset of escape sequences. 3. Python's tokenizer/compiler combo will need to be updated to
work as follows: 1. read the file 2. decode it into Unicode assuming a fixed per-file encoding 3. convert it into a UTF-8 byte string 4. tokenize the UTF-8 content 5. compile it, creating Unicode objects from the given Unicode data
and creating string objects from the Unicode literal data
by first reencoding the UTF-8 data into 8-bit string data
using the given file encoding Note that Python identifiers are restricted to the ASCII
subset of the encoding, and thus need no further conversion
after step 4.

Implementation

    For backwards-compatibility with existing code which currently
uses non-ASCII in string literals without declaring an encoding,
the implementation will be introduced in two phases: 1. Allow non-ASCII in string literals and comments, by internally
treating a missing encoding declaration as a declaration of
"iso-8859-1". This will cause arbitrary byte strings to
correctly round-trip between step 2 and step 5 of the
processing, and provide compatibility with Python 2.2 for
Unicode literals that contain non-ASCII bytes. A warning will be issued if non-ASCII bytes are found in the
input, once per improperly encoded input file. 2. Remove the warning, and change the default encoding to "ascii". The builtin compile() API will be enhanced to accept Unicode as
input. 8-bit string input is subject to the standard procedure for
encoding detection as described above. If a Unicode string with a coding declaration is passed to compile(),
a SyntaxError will be raised. SUZUKI Hisao is working on a patch; see [2] for details. A patch
implementing only phase 1 is available at [1].

Phases

    Implementation of steps 1 and 2 above were completed in 2.3,
except for changing the default encoding to "ascii". The default encoding was set to "ascii" in version 2.5.

Scope

    This PEP intends to provide an upgrade path from the current
(more-or-less) undefined source code encoding situation to a more
robust and portable definition.

References

    [1] Phase 1 implementation:
http://python.org/sf/526840
[2] Phase 2 implementation:
http://python.org/sf/534304

History

    1.10 and above: see CVS history
1.8: Added '.' to the coding RE.
1.7: Added warnings to phase 1 implementation. Replaced the
Latin-1 default encoding with the interpreter's default
encoding. Added tweaks to compile().
1.4 - 1.6: Minor tweaks
1.3: Worked in comments by Martin v. Loewis:
UTF-8 BOM mark detection, Emacs style magic comment,
two phase approach to the implementation

Copyright

    This document has been placed in the public domain.

Source: https://hg.python.org/peps/file/tip/pep-0263.txt

demo:

#!/usr/bin/python
# -*- coding: utf-8 -*-
# from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse('<h1>Hello, this is the first page of my Django(迪亚戈) Web App!</h1>')
# return HttpResponse('<h1>Hello, this is the first page of my Django(迪亚戈) Web App!</h1>')

参考链接:

http://www.crifan.com/python_head_meaning_for_usr_bin_python_coding_utf-8/

https://www.python.org/dev/peps/pep-0263/

https://raw.githubusercontent.com/xgqfrms/Python/master/DjangoApp/main_app/views.py

https://github.com/xgqfrms/Python/blob/gh-pages/DjangoApp/main_app/views.py

# How to get a DOM element's `::before` content with JavaScript?

https://stackoverflow.com/questions/44342065/how-to-get-a-dom-elements-before-content-with-javascript

1

1

 

如何在Python 中使用UTF-8 编码 && Python 使用 注释,Python ,UTF-8 编码 , Python 注释的更多相关文章

  1. Python中字符的编码与解码

    1 文本和字节序列 我们都知道字符串,就是由一些字符组成的序列构成串,那么字符又是什么呢?计算机只能识别二进制的东西,那么计算机又为什么会显示我们的汉字,或者是某个字母呢? 由于最早发明使用计算机是美 ...

  2. 如何在Python中实现这五类强大的概率分布

    R编程语言已经成为统计分析中的事实标准.但在这篇文章中,我将告诉你在Python中实现统计学概念会是如此容易.我要使用Python实现一些离散和连续的概率分布.虽然我不会讨论这些分布的数学细节,但我会 ...

  3. 如何在hadoop中使用外部的python程序文件

    业务场景大概是这样,我需要在公司hadoop集群上对博文进行结巴分词.我的数据是存储在hive表格中的,数据量涉及到五百万用户三个月内发的所有博文. 首先对于数据来说,很简单,在hive表格中就是两列 ...

  4. 【转 记录】python中的encode以及decode

    字符串编码常用类型:utf-8,gb2312,cp936,gbk等. python中,我们使用decode()和encode()来进行解码和编码 在python中,使用unicode类型作为编码的基础 ...

  5. python中字符串前的r什么意思

    Python中,u表示unicode string,表示使用unicode进行编码,没有u表示byte string,类型是str,在没有声明编码方式时,默认ASCI编码.如果要指定编码方式,可在文件 ...

  6. Python中的类(中)

    上一篇介绍了Python中类相关的一些基本点,本文看看Python中类的继承和__slots__属性. 继承 在Python中,同时支持单继承与多继承,一般语法如下: class SubClassNa ...

  7. Python中编写类的各种技巧和方法

    简介 有关 Python 内编写类的各种技巧和方法(构建和初始化.重载操作符.类描述.属性访问控制.自定义序列.反射机制.可调用对象.上下文管理.构建描述符对象.Pickling). 你可以把它当作一 ...

  8. python中pymsql常用方法(1)

    python中pymysql模块常用方法以及其使用 首先我们知道pymysql 是python中操作数据库的模块 使用步骤分为如下几步: ​ 1.与数据库服务器建立链接 conn=pymysql.Co ...

  9. 详解Python中内置的NotImplemented类型的用法

    它是什么? ? 1 2 >>> type(NotImplemented) <type 'NotImplementedType'> NotImplemented 是Pyth ...

  10. 深入理解python(一)python语法总结:基础知识和对python中对象的理解

    用python也用了两年了,趁这次疫情想好好整理下. 大概想法是先对python一些知识点进行总结,之后就是根据python内核源码来对python的实现方式进行学习,不会阅读整个源码,,,但是应该会 ...

随机推荐

  1. 转 7 jmeter之参数化

    7 jmeter之参数化   badboy里参数化(前面4 jmeter badboy脚本开发技术详解已讲过) jmeter里参数化-1 用户参数 1.打开badboy工具,点击红色按钮开始录制,在地 ...

  2. 编程小技巧之 Linux 文本处理命令(二)

    合格的程序员都善于使用工具,正所谓君子性非异也,善假于物也.合理的利用 Linux 的命令行工具,可以提高我们的工作效率. 本篇文章是<Linux 文本处理命令> 续篇,在前文的基础上再介 ...

  3. tcpdump安装与参数详解

    Centos7安装Tcpdump 对于大部分的Linux操作系统,已经默认安装了tcpdump,可以通过以下命令查看: [root@localhost local]# tcpdump --versio ...

  4. CF492B

    题意 一条长为L的路,在n个不同的位置都放置了路灯,灯光半径相同,问半径至少为多少时灯光可以覆盖整条路. 那我们就先排序,使灯的位置是从路的一边依次排到另一边的 ,然后求出两两挨着的灯之间距离的最大值 ...

  5. LOJ10075 农场派对

    USACO 2007 Feb. Silver N(1≤N≤1000) 头牛要去参加一场在编号为 x(1≤x≤N) 的牛的农场举行的派对.有 M(1≤M≤100000) 条有向道路,每条路长Ti​(1≤ ...

  6. Maven环境搭建以及在IDEA中的配置与简单入门

    目录 一.下载与安装 二.配置 1. 环境变量 2. 阿里云镜像 3. 本地仓库 三.IDEA创建Maven项目 1. 创建一个原始的Maven项目 1.2 指定模板创建(可选) 2. 配置GAV 3 ...

  7. Java进阶专题(二十二) 从零开始搭建一个微服务架构系统 (上)

    前言 "微服务"一词源于 Martin Fowler的名为 Microservices的,博文,可以在他的官方博客上找到http:/ /martinfowler . com/art ...

  8. Linux-处理用户输入

    Linux-处理用户输入 1.命令行参数 1.2读取参数 1.3 读取脚本名 1.4测试参数 2.特殊参数变量 2.1 参数统计 2.2抓取所有的数据 3.移动变量 4.处理选项 5.选项标准化 6. ...

  9. Spring boot AOP 实现Redis 存储

    package com.carloan.common.web.annotation; import java.lang.annotation.*; /** * 自定义redis缓存注解.只要在serv ...

  10. 1.VLAN

    1.定位:VLAN,即虚拟局域网(Virtual Local Area Network),一种将局域网设备从逻辑上划分成一个个网段,从而实现虚拟工作组的新兴数据交换技术.VLAN是将一个物理的LAN在 ...