为什么有时候必须添加sys.setdefaultencoding('utf-8')
今天在尝试Python的CGI模块时遇到中文字符不能正确显示的问题,很郁闷.在网上仔细找了找,终于解决了这个问题,现在将解决方法陈述如下,以防下次失误.
页面源代码如下
#-*- coding: utf8 -*-
import cgitb , cgi
cgitb.enable()
form = cgi.FieldStorage()
if (form.has_key("name") and form.has_key("addr")):
    print "<p>name:", form["name"].value
print "<p>addr:", form["addr"].value
[这里仅仅测试addr参数为中文]接收Ascii字符时运行良好,但是接收中文字符时显示乱码,浏览器切换到GB2312编码时
可以正常显示,但是个人要求它成为UTF-8编码显示
改成 print "<p>addr:", form["addr"].value.encode('utf-8') 就报如下错误:
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 0-1: invalid data
在参阅了http://blog.chinaunix.net/u2/68206/showart_668359.html 后终于理解,
Python 里面的编码和解码也就是 unicode 和 str 这两种形式的相互转化。编码是 unicode -> str,相反的,解码就
是 str -> unicode。剩下的问题就是确定何时需要进行编码或者解码了.关于文件开头的"编码指示",也就是 # -*- coding: -*- 这个语句。Python 默认脚本文件都是 UTF-8 编码的,当文件中有非 UTF-8 编码范围内的字符的时候就要使用"编码指示"来修正. 关于 sys.defaultencoding,这个在解码没有明确指明解码方式的时候使用。比如我有如下代码: 
#! /usr/bin/env python 
# -*- coding: utf-8 -*- 
s = '中文'  # 注意这里的 str 是 str 类型的,而不是 unicode 
s.encode('gb18030') 
这句代码将 s 重新编码为 gb18030 的格式,即进行 unicode -> str 的转换。因为 s 本身就是 str 类型的,因此 
Python 会自动的先将 s 解码为 unicode ,然后再编码成 gb18030。因为解码是python自动进行的,我们没有指明解码方式,python 就会使用 sys.defaultencoding 指明的方式来解码。很多情况下 sys.defaultencoding 是 
ANSCII,如果 s 不是这个类型就会出错。拿上面的情况来说,我的 sys.defaultencoding 是 anscii,而 s 的编码方式和文件的编码方式一致,是 utf8 的,所以出错了: 
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 
0: ordinal not in range(128) 
对于这种情况,我们有两种方法来改正错误: 
一是明确的指示出 s 的编码方式 
#! /usr/bin/env python 
# -*- coding: utf-8 -*- 
s = '中文' 
s.decode('utf-8').encode('gb18030') 
二是更改 sys.defaultencoding 为文件的编码方式 
#! /usr/bin/env python 
# -*- coding: utf-8 -*- 
import sys 
reload(sys) # Python2.5 初始化后会删除 sys.setdefaultencoding 这个方法,我们需要重新载入 
sys.setdefaultencoding('utf-8') 
str = '中文' 
str.encode('gb18030')
看完之后,改成这样
print "<p>addr:", form["addr"].value.decode('gb2312').encode('utf-8') 
成功通过.
我总结一下为什么要这么写的原因:
1. 当取回来的数据与你当前脚本中声明的编码不一致时就要做编码转换
2.在编码转换时首先要将该数据以自身编码的格式换成unicode码,再将这个unicode按utf8编码
3.为什么我的浏览器会传回gb2312的编码数据到服务器,这应该和客户端的系统编码有关系
转自:http://vb2005xu.iteye.com/blog/258633
为什么有时候必须添加sys.setdefaultencoding('utf-8')的更多相关文章
- python 默认的系统编码   sys.setdefaultencoding
		python2.x的编码问题有时让人很头疼,一会ascii,一会unicode. 在脚本里多见这样的操作: import sys reload(sys) sys.setdefaultencoding( ... 
- python---补充django中文报错(1),Django2.7使用sys.setdefaultencoding('utf-8'),以及使用reload(sys)原因
		SyntaxError at /blog/ news/story Non-ASCII character , but no encoding declared; see http://python.o ... 
- Python  sys.setdefaultencoding('utf-8') 后就没输出
		为了解决Python的 UnicodeDecodeError: 'ascii' codec can't decode byte ,我们可以加入以下代码. import sys reload(sys) ... 
- python3 中的reload(sys)和sys.setdefaultencoding('utf-8')
		通常我们为了防止出现乱码会进行一下操作 import sys reload(sys) sys.setdefaultencoding('utf-8') 但这是python2的写法,但是在python3中 ... 
- 也来谈谈python编码
		一.coding:utf-8 让我们先来看一个示例,源码文件是utf-8格式: print('你好 python') 当使用python2执行该程序时会收到一下报错: File "./hel ... 
- python中引入包的时候报错AttributeError: module 'sys' has no attribute 'setdefaultencoding'解决方法?
		python中引入包的时候报错:import unittestimport smtplibimport timeimport osimport sysimp.reload(sys)sys.setdef ... 
- python3中报错AttributeError: module 'sys' has no attribute 'setdefaultencoding'
		reload(sys) sys.setdefaultencoding("utf-8") f = open('.\\24.novel.txt','rb') str = f.read( ... 
- (转)Python3异常-AttributeError: module 'sys' has no attribute 'setdefaultencoding
		基于python3.6.1版本,在一个.py文件中,加入这3行:import requests, re, sysreload(sys)sys.setdefaultencoding("utf- ... 
- Python3异常-AttributeError: module 'sys' has no attribute 'setdefaultencoding'
		基于python3.6.1版本,在一个.py文件中,加入这3行: import requests, re, sys reload(sys) sys.setdefaultencoding("u ... 
随机推荐
- JPA 系列教程19-jpa-uuid主键生成策略
			ddl语句 CREATE TABLE `t_user` ( `id` varchar(32) NOT NULL, `name` varchar(255) DEFAULT NULL, PRIMARY K ... 
- bootstrap ch2清除浮动
			<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ... 
- Apache Commons DbUtils 快速上手
			原文出处:http://lavasoft.blog.51cto.com/62575/222771 Hibernate太复杂,iBatis不好用,JDBC代码太垃圾,DBUtils在简单与优美之间取得了 ... 
- 11--Python 备份文件程序
			最近看了下<A Byte of Python>, 看见一个非常有意思的程序,用python进行文件行备份的练习程序, 自己在机器上敲代码运行了一遍,结果出现了一个小问题,路径出错--&qu ... 
- 解决ie阴影的兼容性
			box-shadow:0px 0px 10px #aba25b; -webkit-box-shadow:0px 0px 10px #aba25b; -moz-box-shadow:0px 0px 10 ... 
- hdu_1950_Bridging signals(LIS)
			题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1950 题意:实际就是求最长递增子序列 题解:有两种解法,一种是利用二分,一种是用线段树 这个是这题的二 ... 
- ice使用过程遇到的问题
			1 设置代理超时时间ice_timeout ICE的每个连接都有两个超时时间:ice_timeout.ice_connectiontimeout,分别对应消息的超时时间和连接建立 的超时时间,可 ... 
- LeetCode OJ 220.Contains Duplicate 3
			Given an array of integers, find out whether there are two distinct indices i and j in the array suc ... 
- Razor Engine,动态脚本语言,mvc上的语法,适用于文件内容生成,静态网页生成等。
			https://razorengine.codeplex.com/ 下载并引用:razorengine.dll 代码里这样写,用mvc的人都会!Razor语法! string template = & ... 
- C#常用集合的使用
			大多数集合都在System.Collections,System.Collections.Generic两个命名空间.其中System.Collections.Generic专门用于泛型集合. 针对特 ... 
