今天在尝试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')的更多相关文章

  1. python 默认的系统编码 sys.setdefaultencoding

    python2.x的编码问题有时让人很头疼,一会ascii,一会unicode. 在脚本里多见这样的操作: import sys reload(sys) sys.setdefaultencoding( ...

  2. 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 ...

  3. Python sys.setdefaultencoding('utf-8') 后就没输出

    为了解决Python的 UnicodeDecodeError: 'ascii' codec can't decode byte ,我们可以加入以下代码. import sys reload(sys) ...

  4. python3 中的reload(sys)和sys.setdefaultencoding('utf-8')

    通常我们为了防止出现乱码会进行一下操作 import sys reload(sys) sys.setdefaultencoding('utf-8') 但这是python2的写法,但是在python3中 ...

  5. 也来谈谈python编码

    一.coding:utf-8 让我们先来看一个示例,源码文件是utf-8格式: print('你好 python') 当使用python2执行该程序时会收到一下报错: File "./hel ...

  6. python中引入包的时候报错AttributeError: module 'sys' has no attribute 'setdefaultencoding'解决方法?

    python中引入包的时候报错:import unittestimport smtplibimport timeimport osimport sysimp.reload(sys)sys.setdef ...

  7. python3中报错AttributeError: module 'sys' has no attribute 'setdefaultencoding'

    reload(sys) sys.setdefaultencoding("utf-8") f = open('.\\24.novel.txt','rb') str = f.read( ...

  8. (转)Python3异常-AttributeError: module 'sys' has no attribute 'setdefaultencoding

    基于python3.6.1版本,在一个.py文件中,加入这3行:import requests, re, sysreload(sys)sys.setdefaultencoding("utf- ...

  9. Python3异常-AttributeError: module 'sys' has no attribute 'setdefaultencoding'

    基于python3.6.1版本,在一个.py文件中,加入这3行: import requests, re, sys reload(sys) sys.setdefaultencoding("u ...

随机推荐

  1. 美化type="file"控件

    对于type="file"而言是一个不大好看的控件.如果不美化他一下,总感觉自己的网站有些缺乏了美感 上代码: <!DOCTYPE html> <html> ...

  2. hdu5573 二叉树找规律,二进制相关

    input T 1<=T<=100 n k 1<=n<=1e9  n<=2^k<=2^60 output 从1走到第k层,下一层的数是上一层的数×2或者×2+1,每 ...

  3. android之ViewStub的使用

    转自http://blog.csdn.net/hitlion2008/article/details/6737537 在开发应用程序的时候,经常会遇到这样的情况,会在运行时动态根据条件来决定显示哪个V ...

  4. 搭建Mantis 缺陷管理系统

    什么是Mantis MantisBT is a free popular web-based bugtracking system (feature list). It is written in t ...

  5. Linux Shell 小脚本经典收藏

    原文:http://www.cnblogs.com/Javame/p/3867686.html 1.在两个文件中找出相同的号码 diff -y xx.txt oo.txt | egrep -v &qu ...

  6. 其他应用和技巧-用Json格式来保存数据

    -------------------- <script type="text/javascript">            //定义json变量           ...

  7. python unitest基本

    基本 import unittest class OneTest(unittest.TestCase): def setUp(self): self.verificationErrors = [] s ...

  8. mysql 注册登陆表单并且操纵元素

    <?php      error_reporting(E_ALL^E_DEPRECATED^E_NOTICE);    header("content-type:text/html;c ...

  9. getTime 方法

    转自http://www.yesky.com/imagesnew/software/javascript/html/jsmthgettime.htm getTime 方法返回 Date 对象中的时间值 ...

  10. [tableView dequeueReusableCellWithIdentifier:CellIdentifier] 后面forIndexPath:indexPath参数的解释

    解决以下错误: *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'u ...