Skip to content

Commit d6bfcf4

Browse files
committed
中文编码问题
decode,encode,url编码 >>> urllib.quote(data) '%E4%B8%BD%E6%B1%9F' 那我们想转回去呢? >>> urllib.unquote('%E4%B8%BD%E6%B1%9F') '\xe4\xb8\xbd\xe6\xb1\x9f' >>> print urllib.unquote('%E4%B8%BD%E6%B1%9F') 丽江
1 parent df027c7 commit d6bfcf4

21 files changed

+3949
-51
lines changed

decode_encode/decode_encode.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# -*- coding: utf-8 -*-
2+
#---------------------------------
3+
# Python编码介绍——encode和decode
4+
#### 将http://blog.chinaunix.net/uid-27838438-id-4227131.html
5+
# 将其他编码的字符串解码(decode)成 Unicode
6+
# 再从 Unicode编码(encode)成另一种编码
7+
# 对 Unicode 进行编码和对 str 进行编码都是错误的
8+
# 要在同一个文本中进行两种编码的输出等操作就必须进行编码的转换,
9+
# 先用decode将文本原来的编码转换成Unicode,再用encode将编码转换成需要转换成的编码。
10+
# http://www.jb51.net/article/55759.htm
11+
'''
12+
python中如何避免中文是乱码
13+
这个问题是一个具有很强操作性的问题。我这里有一个经验总结,分享一下,供参考:
14+
首先,提倡使用utf-8编码方案,因为它跨平台不错。
15+
经验一:在开头声明:
16+
# -*- coding: utf-8 -*-
17+
有朋友问我-*-有什么作用,那个就是为了好看,爱美之心人皆有,更何况程序员?当然,也可以写成:
18+
# coding:utf-8
19+
经验二:遇到字符(节)串,立刻转化为unicode,不要用str(),直接使用unicode()
20+
unicode_str = unicode('中文', encoding='utf-8')
21+
print unicode_str.encode('utf-8')
22+
经验三:如果对文件操作,打开文件的时候,最好用codecs.open,替代open(这个后面会讲到,先放在这里)
23+
import codecs
24+
codecs.open('filename', encoding='utf8')
25+
我还收集了网上的一片文章,也挺好的,推荐给看官:Python2.x的中文显示方法
26+
最后告诉给我,如果用python3,坑爹的编码问题就不烦恼了。
27+
'''
28+
29+
#---------------------------------
30+
31+
fp1 = open('test1.txt', 'r')
32+
info1 = fp1.read()
33+
# 已知是 GBK 编码,解码成 Unicode
34+
# 内置函数 open() 打开文件时,read() 读取的是 str,读取后需要使用正确的编码格式进行 decode()
35+
tmp = info1.decode('GBK')
36+
# print 'tmp:',tmp
37+
38+
fp2 = open('test2.txt', 'w')
39+
# 编码成 UTF-8 编码的 str
40+
info2 = tmp.encode('UTF-8')
41+
# print 'info2:',info2
42+
fp2.write(info2)
43+
fp2.close()
44+
45+
# 获取编码的方式:
46+
# 判断是 s 字符串否为Unicode,如果是返回True,不是返回False
47+
print 'tmp is unicode?'+str(isinstance(tmp, unicode))
48+
print 'info2 is unicode?'+str(isinstance(info2, unicode))
49+
50+
# 下面代码可以获取系统默认编码:
51+
import sys
52+
print sys.getdefaultencoding()
53+
54+
# 经验二:遇到字符(节)串,立刻转化为unicode,不要用str(),直接使用unicode()
55+
unicode_str = unicode('中文', encoding='utf-8')
56+
print unicode_str.encode('utf-8')
57+
58+
# 经验三:如果对文件操作,打开文件的时候,最好用codecs.open,替代open(这个后面会讲到,先放在这里)
59+
import codecs
60+
codecs.open('filename', encoding='utf8')
61+
62+
63+
#######非常重要
64+
#####http://dashen2009.blog.51cto.com/714741/199157
65+
# >>> urllib.quote(data)
66+
# '%E4%B8%BD%E6%B1%9F'
67+
# 那我们想转回去呢?
68+
# >>> urllib.unquote('%E4%B8%BD%E6%B1%9F')
69+
# '\xe4\xb8\xbd\xe6\xb1\x9f'

decode_encode/test1.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# -*- coding: utf-8 -*-
2+
#---------------------------------
3+
# Python������ܡ���encode��decode
4+
#### ��http://blog.chinaunix.net/uid-27838438-id-4227131.html
5+
# ������������ַ������루decode���� Unicode
6+
# �ٴ� Unicode���루encode������һ�ֱ���
7+
# �� Unicode ���б���Ͷ� str ���б��붼�Ǵ����
8+
# Ҫ��ͬһ���ı��н������ֱ��������Ȳ����ͱ�����б����ת����
9+
# ����decode���ı�ԭ���ı���ת����Unicode������encode������ת������Ҫת���ɵı��롣
10+
#---------------------------------

decode_encode/test2.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# -*- coding: utf-8 -*-
2+
#---------------------------------
3+
# Python编码介绍——encode和decode
4+
#### 将http://blog.chinaunix.net/uid-27838438-id-4227131.html
5+
# 将其他编码的字符串解码(decode)成 Unicode
6+
# 再从 Unicode编码(encode)成另一种编码
7+
# 对 Unicode 进行编码和对 str 进行编码都是错误的
8+
# 要在同一个文本中进行两种编码的输出等操作就必须进行编码的转换,
9+
# 先用decode将文本原来的编码转换成Unicode,再用encode将编码转换成需要转换成的编码。
10+
#---------------------------------

google_api/.idea/google_api.iml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

google_api/.idea/misc.xml

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

google_api/.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)