encode是指将unicode字符编码成其他字符集的字符,如utf-8,ascii等;

而decode是指将其他字符编码,如utf-8转换成unicode编码。

encode是指将人类用的语言(字符串)编码为机器能识别的语言(字节码),decode反之。

【简单来说编码就是把人类通用的语言符号翻译成计算机通用的对象,而反向的翻译过程自然就是解码了。Python 中的字符串类型代表人类通用的语言符号,因此字符串类型有encode()方法;而字节类型代表计算机通用的对象(二进制数据),因此字节类型有decode()方法。】

引自:http://python.jobbole.com/84840/

其中还包含更多关于字符集的解释,和类似'ascii' codec can't encode character '\u96e8' in position 0..的错误解决方法

python中可以用isinstance函数来判断某个字符串是否是unicode:

s=u"中文"

isinstance(s, unicode) #用来判断是否为unicode

给出一段处理字符串编码的通用代码:

    if isinstance(s, unicode):
s=s.encode('utf-8')
     #如果当前是unicode,就直接编码成utf-8
print 'encoded'
else:
s=s.decode('gb2312').encode('utf-8')
#否则,已知当前是gb2312字符集,先将其解码成unicode,再按照utf-8编码
print 'decoded and encoded'

引自http://www.blog.chinaunix.net/uid-25063573-id-3033365.html

encode与decode,unicode与中文乱码的问题的更多相关文章

  1. Python字符串的encode与decode研究心得——解决乱码问题

    转~Python字符串的encode与decode研究心得——解决乱码问题 为什么Python使用过程中会出现各式各样的乱码问题,明明是中文字符却显示成“/xe4/xb8/xad/xe6/x96/x8 ...

  2. unicode、encode、decode

    1.encode与decode:unicode经过encode -> utf-8,反过来为decode. 爬虫读取网页内容和pandas读取csv时,会把读取到的文字内容转成unicode,当我 ...

  3. 在python3 encode和decode 的使用

    说这个问题之前必须的介绍关于编码的在我们这的发展: 首先电脑能识别的最初的语言是二进制 ---010101这种 然后在是我们知道的ASSIC码 再过了就是 gb2312----------->g ...

  4. 【python】python新手必碰到的问题---encode与decode,中文乱码[转]

    转自:http://blog.csdn.net/a921800467b/article/details/8579510 为什么会报错“UnicodeEncodeError:'ascii' codec ...

  5. Python 关于 encode与decode 中文乱码问题

    字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unicode编码(en ...

  6. [转]python新手必碰到的问题---encode与decode,中文乱码--转载

    edu.codepub.com/2009/1029/17037.php 这个问题在python3.0里已经解决了. 这有篇很好的文章,可以明白这个问题: 为什么会报错“UnicodeEncodeErr ...

  7. URL地址中中文乱码详解(javascript中encodeURI和decodeURI方法、java.net.URLDecoder.encode、java.net.URLDecoder.decode)

    引言: 在Restful类的服务设计中,经常会碰到需要在URL地址中使用中文作为的参数的情况,这种情况下,一般都需要正确的设置和编码中文字符信息.乱码问题就此产生了,该如何解决呢?且听本文详细道来. ...

  8. python+sublime text2中文乱码[Decode error - output not utf-8]

    转自: http://blog.sina.com.cn/s/blog_765abd7b0101dtbw.html 学习,记录一下.中文编码真的挺麻烦.抽空把自己的sb3的配置写一些. 该问题让我纠结了 ...

  9. UnicodeEncodeError: 'latin-1' codec can't encode characters,python3 中文乱码

    UnicodeEncodeError: 'latin-1' codec can't encode characters in position 9-13: ordinal not in range(2 ...

随机推荐

  1. beta汇总

    第一天:http://www.cnblogs.com/hxh969012806/p/5034085.html 第二天:http://www.cnblogs.com/zyk150910/p/503783 ...

  2. CSS重点巩固

    一:position定位 a: static 定位 ,HTML元素的默认值,即没有定位,元素出现在正常的流中.静态定位的元素不会受到top, bottom, left, right影响. b: fix ...

  3. spring aop开发常见错误

    1. Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreExcepti ...

  4. java.lang.NoClassDefFoundError: org/objectweb/asm/Type

    Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/objectweb/asm/ ...

  5. java基础-关键字-native

     一. 什么是Native Method    简单地讲,一个Native Method就是一个java调用非java代码的接口.一个Native Method是这样一个java的方法:该方法的实现由 ...

  6. zoj3819Average Score

    Average Score Time Limit: 2 Seconds      Memory Limit: 65536 KB Bob is a freshman in Marjar Universi ...

  7. MyEclipse------黑科技

    自动计算器(+,-,*,/) <form method="post" oninput="o.value = parseInt(a.value) + parseInt ...

  8. boost(barrier)

    barrier:栅栏的意思,当barrier bar(3),这三个线程会放到栅栏中,等第三个线程执行时一起唤醒,然后返回 barrier barrier类的接口定义如下: class barrier ...

  9. tp框架做留言板

    首先是登录的LoginController.class.php 代码内容是 <?php namespace Admin\Controller; use Think\Controller; cla ...

  10. 锋利的jQuery-2--判断jQuery获取到的对象是否存在$().length

    1.使用js获取不存在的对象: document.getElementById("tt").style.color = "red"; 如果网页中不存在id = ...